{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy import stats"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 读取数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>客户编号</th>\n",
       "      <th>已发货款</th>\n",
       "      <th>资产成本</th>\n",
       "      <th>贷款与资产比列</th>\n",
       "      <th>品牌</th>\n",
       "      <th>骑车销售商</th>\n",
       "      <th>车厂</th>\n",
       "      <th>出生日期</th>\n",
       "      <th>货款日期</th>\n",
       "      <th>地区</th>\n",
       "      <th>...</th>\n",
       "      <th>尚未还清有效贷款总额</th>\n",
       "      <th>已批准贷款总额</th>\n",
       "      <th>已发放贷款总额</th>\n",
       "      <th>每月还款总额</th>\n",
       "      <th>贷款与已还贷款比列</th>\n",
       "      <th>主账户还款期数</th>\n",
       "      <th>次账户还款期数</th>\n",
       "      <th>贷款与已批准贷款比列</th>\n",
       "      <th>总贷款次数与总有效贷款次数比</th>\n",
       "      <th>工作类型</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>601758</td>\n",
       "      <td>65532</td>\n",
       "      <td>78990</td>\n",
       "      <td>84.38</td>\n",
       "      <td>136</td>\n",
       "      <td>20490</td>\n",
       "      <td>45</td>\n",
       "      <td>1981</td>\n",
       "      <td>2018</td>\n",
       "      <td>8</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>519488</td>\n",
       "      <td>56759</td>\n",
       "      <td>65325</td>\n",
       "      <td>89.55</td>\n",
       "      <td>61</td>\n",
       "      <td>22778</td>\n",
       "      <td>86</td>\n",
       "      <td>1967</td>\n",
       "      <td>2018</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>2054139</td>\n",
       "      <td>2036500</td>\n",
       "      <td>2036500</td>\n",
       "      <td>34455</td>\n",
       "      <td>0.99</td>\n",
       "      <td>59</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.33</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>447579</td>\n",
       "      <td>58413</td>\n",
       "      <td>67960</td>\n",
       "      <td>89.02</td>\n",
       "      <td>5</td>\n",
       "      <td>15663</td>\n",
       "      <td>86</td>\n",
       "      <td>1977</td>\n",
       "      <td>2018</td>\n",
       "      <td>9</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>648134</td>\n",
       "      <td>72317</td>\n",
       "      <td>99750</td>\n",
       "      <td>73.68</td>\n",
       "      <td>76</td>\n",
       "      <td>17242</td>\n",
       "      <td>48</td>\n",
       "      <td>1995</td>\n",
       "      <td>2018</td>\n",
       "      <td>8</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>13813</td>\n",
       "      <td>13813</td>\n",
       "      <td>0</td>\n",
       "      <td>13814.00</td>\n",
       "      <td>13813</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>458210</td>\n",
       "      <td>50078</td>\n",
       "      <td>65450</td>\n",
       "      <td>79.45</td>\n",
       "      <td>146</td>\n",
       "      <td>14181</td>\n",
       "      <td>45</td>\n",
       "      <td>1974</td>\n",
       "      <td>2018</td>\n",
       "      <td>17</td>\n",
       "      <td>...</td>\n",
       "      <td>467161</td>\n",
       "      <td>550000</td>\n",
       "      <td>550000</td>\n",
       "      <td>12863</td>\n",
       "      <td>1.18</td>\n",
       "      <td>42</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.06</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 49 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     客户编号   已发货款   资产成本  贷款与资产比列   品牌  骑车销售商  车厂  出生日期  货款日期  地区  ...  \\\n",
       "0  601758  65532  78990    84.38  136  20490  45  1981  2018   8  ...   \n",
       "1  519488  56759  65325    89.55   61  22778  86  1967  2018   6  ...   \n",
       "2  447579  58413  67960    89.02    5  15663  86  1977  2018   9  ...   \n",
       "3  648134  72317  99750    73.68   76  17242  48  1995  2018   8  ...   \n",
       "4  458210  50078  65450    79.45  146  14181  45  1974  2018  17  ...   \n",
       "\n",
       "   尚未还清有效贷款总额  已批准贷款总额  已发放贷款总额  每月还款总额  贷款与已还贷款比列  主账户还款期数  次账户还款期数  \\\n",
       "0           0        0        0       0       1.00        0        0   \n",
       "1     2054139  2036500  2036500   34455       0.99       59        0   \n",
       "2           0        0        0       0       1.00        0        0   \n",
       "3           0    13813    13813       0   13814.00    13813        0   \n",
       "4      467161   550000   550000   12863       1.18       42        0   \n",
       "\n",
       "   贷款与已批准贷款比列  总贷款次数与总有效贷款次数比  工作类型  \n",
       "0         1.0            1.00     0  \n",
       "1         1.0            1.33     1  \n",
       "2         1.0            1.00     1  \n",
       "3         1.0            2.00     0  \n",
       "4         1.0            1.06     1  \n",
       "\n",
       "[5 rows x 49 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info = pd.read_csv(r\"./车贷违约预测.csv\",encoding = 'GBK')\n",
    "user_info.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "查看数据统计描述"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>客户编号</th>\n",
       "      <th>已发货款</th>\n",
       "      <th>资产成本</th>\n",
       "      <th>贷款与资产比列</th>\n",
       "      <th>品牌</th>\n",
       "      <th>骑车销售商</th>\n",
       "      <th>车厂</th>\n",
       "      <th>出生日期</th>\n",
       "      <th>货款日期</th>\n",
       "      <th>地区</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.0</td>\n",
       "      <td>199717.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>535690.886665</td>\n",
       "      <td>54256.272280</td>\n",
       "      <td>7.582391e+04</td>\n",
       "      <td>74.643960</td>\n",
       "      <td>72.698508</td>\n",
       "      <td>19634.049665</td>\n",
       "      <td>69.085766</td>\n",
       "      <td>1983.876921</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>7.245222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>68193.411418</td>\n",
       "      <td>12977.656996</td>\n",
       "      <td>1.892894e+04</td>\n",
       "      <td>11.490485</td>\n",
       "      <td>69.706185</td>\n",
       "      <td>3493.655400</td>\n",
       "      <td>22.128288</td>\n",
       "      <td>9.805565</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.481338</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>417428.000000</td>\n",
       "      <td>13320.000000</td>\n",
       "      <td>3.700000e+04</td>\n",
       "      <td>10.030000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>10524.000000</td>\n",
       "      <td>45.000000</td>\n",
       "      <td>1949.000000</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>476762.000000</td>\n",
       "      <td>46977.000000</td>\n",
       "      <td>6.571400e+04</td>\n",
       "      <td>68.730000</td>\n",
       "      <td>14.000000</td>\n",
       "      <td>16505.000000</td>\n",
       "      <td>48.000000</td>\n",
       "      <td>1977.000000</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>4.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>535571.000000</td>\n",
       "      <td>53703.000000</td>\n",
       "      <td>7.092200e+04</td>\n",
       "      <td>76.670000</td>\n",
       "      <td>61.000000</td>\n",
       "      <td>20333.000000</td>\n",
       "      <td>86.000000</td>\n",
       "      <td>1986.000000</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>6.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>594571.000000</td>\n",
       "      <td>60247.000000</td>\n",
       "      <td>7.915900e+04</td>\n",
       "      <td>83.590000</td>\n",
       "      <td>130.000000</td>\n",
       "      <td>23000.000000</td>\n",
       "      <td>86.000000</td>\n",
       "      <td>1992.000000</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>10.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>671084.000000</td>\n",
       "      <td>990572.000000</td>\n",
       "      <td>1.628992e+06</td>\n",
       "      <td>95.000000</td>\n",
       "      <td>261.000000</td>\n",
       "      <td>24803.000000</td>\n",
       "      <td>156.000000</td>\n",
       "      <td>2000.000000</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>22.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                客户编号           已发货款          资产成本        贷款与资产比列  \\\n",
       "count  199717.000000  199717.000000  1.997170e+05  199717.000000   \n",
       "mean   535690.886665   54256.272280  7.582391e+04      74.643960   \n",
       "std     68193.411418   12977.656996  1.892894e+04      11.490485   \n",
       "min    417428.000000   13320.000000  3.700000e+04      10.030000   \n",
       "25%    476762.000000   46977.000000  6.571400e+04      68.730000   \n",
       "50%    535571.000000   53703.000000  7.092200e+04      76.670000   \n",
       "75%    594571.000000   60247.000000  7.915900e+04      83.590000   \n",
       "max    671084.000000  990572.000000  1.628992e+06      95.000000   \n",
       "\n",
       "                  品牌          骑车销售商             车厂           出生日期      货款日期  \\\n",
       "count  199717.000000  199717.000000  199717.000000  199717.000000  199717.0   \n",
       "mean       72.698508   19634.049665      69.085766    1983.876921    2018.0   \n",
       "std        69.706185    3493.655400      22.128288       9.805565       0.0   \n",
       "min         1.000000   10524.000000      45.000000    1949.000000    2018.0   \n",
       "25%        14.000000   16505.000000      48.000000    1977.000000    2018.0   \n",
       "50%        61.000000   20333.000000      86.000000    1986.000000    2018.0   \n",
       "75%       130.000000   23000.000000      86.000000    1992.000000    2018.0   \n",
       "max       261.000000   24803.000000     156.000000    2000.000000    2018.0   \n",
       "\n",
       "                  地区  \n",
       "count  199717.000000  \n",
       "mean        7.245222  \n",
       "std         4.481338  \n",
       "min         1.000000  \n",
       "25%         4.000000  \n",
       "50%         6.000000  \n",
       "75%        10.000000  \n",
       "max        22.000000  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 统计类描述\n",
    "user_info.describe().iloc[:,:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>对接员工编号</th>\n",
       "      <th>是否填写手机号</th>\n",
       "      <th>受否填写身份证</th>\n",
       "      <th>是否出具驾驶证</th>\n",
       "      <th>是否填写护照</th>\n",
       "      <th>信用评分</th>\n",
       "      <th>主账户贷款次数</th>\n",
       "      <th>主账户有效贷款次数</th>\n",
       "      <th>主账户中尚未还清有效贷款</th>\n",
       "      <th>主账户中已批准的贷款</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.0</td>\n",
       "      <td>199717.0</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>1.997170e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>1547.857919</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.023348</td>\n",
       "      <td>0.002143</td>\n",
       "      <td>291.762544</td>\n",
       "      <td>2.464037</td>\n",
       "      <td>1.048414</td>\n",
       "      <td>1.687286e+05</td>\n",
       "      <td>2.224323e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>974.901476</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.151007</td>\n",
       "      <td>0.046243</td>\n",
       "      <td>339.317591</td>\n",
       "      <td>5.283968</td>\n",
       "      <td>1.951018</td>\n",
       "      <td>9.638043e+05</td>\n",
       "      <td>2.522528e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-6.678296e+06</td>\n",
       "      <td>0.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>712.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>1449.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>14.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>2357.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>680.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>3.589900e+04</td>\n",
       "      <td>6.400000e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>3795.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>890.000000</td>\n",
       "      <td>453.000000</td>\n",
       "      <td>144.000000</td>\n",
       "      <td>9.652492e+07</td>\n",
       "      <td>1.000000e+09</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              对接员工编号   是否填写手机号   受否填写身份证        是否出具驾驶证         是否填写护照  \\\n",
       "count  199717.000000  199717.0  199717.0  199717.000000  199717.000000   \n",
       "mean     1547.857919       1.0       1.0       0.023348       0.002143   \n",
       "std       974.901476       0.0       0.0       0.151007       0.046243   \n",
       "min         1.000000       1.0       1.0       0.000000       0.000000   \n",
       "25%       712.000000       1.0       1.0       0.000000       0.000000   \n",
       "50%      1449.000000       1.0       1.0       0.000000       0.000000   \n",
       "75%      2357.000000       1.0       1.0       0.000000       0.000000   \n",
       "max      3795.000000       1.0       1.0       1.000000       1.000000   \n",
       "\n",
       "                信用评分        主账户贷款次数      主账户有效贷款次数  主账户中尚未还清有效贷款    主账户中已批准的贷款  \n",
       "count  199717.000000  199717.000000  199717.000000  1.997170e+05  1.997170e+05  \n",
       "mean      291.762544       2.464037       1.048414  1.687286e+05  2.224323e+05  \n",
       "std       339.317591       5.283968       1.951018  9.638043e+05  2.522528e+06  \n",
       "min         0.000000       0.000000       0.000000 -6.678296e+06  0.000000e+00  \n",
       "25%         0.000000       0.000000       0.000000  0.000000e+00  0.000000e+00  \n",
       "50%        14.000000       1.000000       0.000000  0.000000e+00  0.000000e+00  \n",
       "75%       680.000000       3.000000       1.000000  3.589900e+04  6.400000e+04  \n",
       "max       890.000000     453.000000     144.000000  9.652492e+07  1.000000e+09  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info.describe().iloc[:,10:20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>主账户中已发放贷款</th>\n",
       "      <th>次账户贷款次数</th>\n",
       "      <th>次账户有效贷款次数</th>\n",
       "      <th>次账户中尚未还清有效贷款</th>\n",
       "      <th>次账户中已批准贷款</th>\n",
       "      <th>次账户中已发放贷款</th>\n",
       "      <th>主账户每月还款</th>\n",
       "      <th>次账户没用还款</th>\n",
       "      <th>近六个月新贷款次数</th>\n",
       "      <th>近六个月违约次数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>2.220420e+05</td>\n",
       "      <td>0.059524</td>\n",
       "      <td>0.027689</td>\n",
       "      <td>5.583871e+03</td>\n",
       "      <td>7.490970e+03</td>\n",
       "      <td>7.374478e+03</td>\n",
       "      <td>1.314415e+04</td>\n",
       "      <td>3.013734e+02</td>\n",
       "      <td>0.385070</td>\n",
       "      <td>0.095956</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>2.525814e+06</td>\n",
       "      <td>0.630648</td>\n",
       "      <td>0.314428</td>\n",
       "      <td>1.686728e+05</td>\n",
       "      <td>1.818362e+05</td>\n",
       "      <td>1.812332e+05</td>\n",
       "      <td>1.524289e+05</td>\n",
       "      <td>1.304531e+04</td>\n",
       "      <td>0.957339</td>\n",
       "      <td>0.380935</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-5.746470e+05</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>6.200000e+04</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>2.000000e+03</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1.000000e+09</td>\n",
       "      <td>52.000000</td>\n",
       "      <td>36.000000</td>\n",
       "      <td>3.603285e+07</td>\n",
       "      <td>2.688820e+07</td>\n",
       "      <td>2.688820e+07</td>\n",
       "      <td>2.564281e+07</td>\n",
       "      <td>3.246710e+06</td>\n",
       "      <td>35.000000</td>\n",
       "      <td>20.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          主账户中已发放贷款        次账户贷款次数      次账户有效贷款次数  次账户中尚未还清有效贷款     次账户中已批准贷款  \\\n",
       "count  1.997170e+05  199717.000000  199717.000000  1.997170e+05  1.997170e+05   \n",
       "mean   2.220420e+05       0.059524       0.027689  5.583871e+03  7.490970e+03   \n",
       "std    2.525814e+06       0.630648       0.314428  1.686728e+05  1.818362e+05   \n",
       "min    0.000000e+00       0.000000       0.000000 -5.746470e+05  0.000000e+00   \n",
       "25%    0.000000e+00       0.000000       0.000000  0.000000e+00  0.000000e+00   \n",
       "50%    0.000000e+00       0.000000       0.000000  0.000000e+00  0.000000e+00   \n",
       "75%    6.200000e+04       0.000000       0.000000  0.000000e+00  0.000000e+00   \n",
       "max    1.000000e+09      52.000000      36.000000  3.603285e+07  2.688820e+07   \n",
       "\n",
       "          次账户中已发放贷款       主账户每月还款       次账户没用还款      近六个月新贷款次数       近六个月违约次数  \n",
       "count  1.997170e+05  1.997170e+05  1.997170e+05  199717.000000  199717.000000  \n",
       "mean   7.374478e+03  1.314415e+04  3.013734e+02       0.385070       0.095956  \n",
       "std    1.812332e+05  1.524289e+05  1.304531e+04       0.957339       0.380935  \n",
       "min    0.000000e+00  0.000000e+00  0.000000e+00       0.000000       0.000000  \n",
       "25%    0.000000e+00  0.000000e+00  0.000000e+00       0.000000       0.000000  \n",
       "50%    0.000000e+00  0.000000e+00  0.000000e+00       0.000000       0.000000  \n",
       "75%    0.000000e+00  2.000000e+03  0.000000e+00       0.000000       0.000000  \n",
       "max    2.688820e+07  2.564281e+07  3.246710e+06      35.000000      20.000000  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info.describe().iloc[:,20:30]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>平均贷款期限</th>\n",
       "      <th>第一次贷款距今时间</th>\n",
       "      <th>贷款查询次数</th>\n",
       "      <th>是否违约</th>\n",
       "      <th>贷款与资产比</th>\n",
       "      <th>贷款总次数</th>\n",
       "      <th>主账户无效贷款次数</th>\n",
       "      <th>次账户无效贷款次数</th>\n",
       "      <th>无效贷款总次数</th>\n",
       "      <th>尚未还清有效贷款总额</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>1.997170e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>8.058107</td>\n",
       "      <td>13.190875</td>\n",
       "      <td>0.203338</td>\n",
       "      <td>0.177391</td>\n",
       "      <td>0.723575</td>\n",
       "      <td>2.523561</td>\n",
       "      <td>1.415623</td>\n",
       "      <td>0.031835</td>\n",
       "      <td>1.447458</td>\n",
       "      <td>1.743125e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>13.860761</td>\n",
       "      <td>21.156865</td>\n",
       "      <td>0.694087</td>\n",
       "      <td>0.382000</td>\n",
       "      <td>0.113613</td>\n",
       "      <td>5.356066</td>\n",
       "      <td>4.038380</td>\n",
       "      <td>0.412795</td>\n",
       "      <td>4.075544</td>\n",
       "      <td>9.813640e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.094638</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-6.678296e+06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.664431</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.741715</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>13.000000</td>\n",
       "      <td>20.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.809512</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>3.818900e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>117.000000</td>\n",
       "      <td>117.000000</td>\n",
       "      <td>28.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.937987</td>\n",
       "      <td>453.000000</td>\n",
       "      <td>451.000000</td>\n",
       "      <td>42.000000</td>\n",
       "      <td>451.000000</td>\n",
       "      <td>9.652492e+07</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              平均贷款期限      第一次贷款距今时间         贷款查询次数           是否违约  \\\n",
       "count  199717.000000  199717.000000  199717.000000  199717.000000   \n",
       "mean        8.058107      13.190875       0.203338       0.177391   \n",
       "std        13.860761      21.156865       0.694087       0.382000   \n",
       "min         0.000000       0.000000       0.000000       0.000000   \n",
       "25%         0.000000       0.000000       0.000000       0.000000   \n",
       "50%         0.000000       0.000000       0.000000       0.000000   \n",
       "75%        13.000000      20.000000       0.000000       0.000000   \n",
       "max       117.000000     117.000000      28.000000       1.000000   \n",
       "\n",
       "              贷款与资产比          贷款总次数      主账户无效贷款次数      次账户无效贷款次数  \\\n",
       "count  199717.000000  199717.000000  199717.000000  199717.000000   \n",
       "mean        0.723575       2.523561       1.415623       0.031835   \n",
       "std         0.113613       5.356066       4.038380       0.412795   \n",
       "min         0.094638       0.000000       0.000000       0.000000   \n",
       "25%         0.664431       0.000000       0.000000       0.000000   \n",
       "50%         0.741715       1.000000       0.000000       0.000000   \n",
       "75%         0.809512       3.000000       1.000000       0.000000   \n",
       "max         0.937987     453.000000     451.000000      42.000000   \n",
       "\n",
       "             无效贷款总次数    尚未还清有效贷款总额  \n",
       "count  199717.000000  1.997170e+05  \n",
       "mean        1.447458  1.743125e+05  \n",
       "std         4.075544  9.813640e+05  \n",
       "min         0.000000 -6.678296e+06  \n",
       "25%         0.000000  0.000000e+00  \n",
       "50%         0.000000  0.000000e+00  \n",
       "75%         1.000000  3.818900e+04  \n",
       "max       451.000000  9.652492e+07  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info.describe().iloc[:,30:40]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>已批准贷款总额</th>\n",
       "      <th>已发放贷款总额</th>\n",
       "      <th>每月还款总额</th>\n",
       "      <th>贷款与已还贷款比列</th>\n",
       "      <th>主账户还款期数</th>\n",
       "      <th>次账户还款期数</th>\n",
       "      <th>贷款与已批准贷款比列</th>\n",
       "      <th>总贷款次数与总有效贷款次数比</th>\n",
       "      <th>工作类型</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>199717.00</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>2.299233e+05</td>\n",
       "      <td>2.294165e+05</td>\n",
       "      <td>1.344553e+04</td>\n",
       "      <td>inf</td>\n",
       "      <td>5.059582e+04</td>\n",
       "      <td>2.928000e+03</td>\n",
       "      <td>5.535709e+02</td>\n",
       "      <td>1.438913</td>\n",
       "      <td>0.487475</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>2.530977e+06</td>\n",
       "      <td>2.534185e+06</td>\n",
       "      <td>1.531618e+05</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.275670e+06</td>\n",
       "      <td>1.065410e+05</td>\n",
       "      <td>1.141343e+05</td>\n",
       "      <td>0.792213</td>\n",
       "      <td>0.561915</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>-110000.33</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>6.720600e+04</td>\n",
       "      <td>6.508500e+04</td>\n",
       "      <td>2.094000e+03</td>\n",
       "      <td>1.26</td>\n",
       "      <td>2.500000e+01</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>1.670000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1.000000e+09</td>\n",
       "      <td>1.000000e+09</td>\n",
       "      <td>2.564281e+07</td>\n",
       "      <td>inf</td>\n",
       "      <td>1.000000e+09</td>\n",
       "      <td>1.980000e+07</td>\n",
       "      <td>5.000000e+07</td>\n",
       "      <td>18.000000</td>\n",
       "      <td>2.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            已批准贷款总额       已发放贷款总额        每月还款总额  贷款与已还贷款比列       主账户还款期数  \\\n",
       "count  1.997170e+05  1.997170e+05  1.997170e+05  199717.00  1.997170e+05   \n",
       "mean   2.299233e+05  2.294165e+05  1.344553e+04        inf  5.059582e+04   \n",
       "std    2.530977e+06  2.534185e+06  1.531618e+05        NaN  2.275670e+06   \n",
       "min    0.000000e+00  0.000000e+00  0.000000e+00 -110000.33  0.000000e+00   \n",
       "25%    0.000000e+00  0.000000e+00  0.000000e+00       1.00  0.000000e+00   \n",
       "50%    0.000000e+00  0.000000e+00  0.000000e+00       1.00  0.000000e+00   \n",
       "75%    6.720600e+04  6.508500e+04  2.094000e+03       1.26  2.500000e+01   \n",
       "max    1.000000e+09  1.000000e+09  2.564281e+07        inf  1.000000e+09   \n",
       "\n",
       "            次账户还款期数    贷款与已批准贷款比列  总贷款次数与总有效贷款次数比           工作类型  \n",
       "count  1.997170e+05  1.997170e+05   199717.000000  199717.000000  \n",
       "mean   2.928000e+03  5.535709e+02        1.438913       0.487475  \n",
       "std    1.065410e+05  1.141343e+05        0.792213       0.561915  \n",
       "min    0.000000e+00  0.000000e+00        1.000000       0.000000  \n",
       "25%    0.000000e+00  1.000000e+00        1.000000       0.000000  \n",
       "50%    0.000000e+00  1.000000e+00        1.000000       0.000000  \n",
       "75%    0.000000e+00  1.000000e+00        1.670000       1.000000  \n",
       "max    1.980000e+07  5.000000e+07       18.000000       2.000000  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info.describe().iloc[:,40:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 异常值处理 缺失值处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据有效性 没贷款的人没有预测价值 谈不上违约\n",
    "user_info = user_info[user_info['贷款总次数'] != 0]\n",
    "\n",
    "#查看有无重复 ： 无\n",
    "user_info.duplicated().sum()\n",
    "\n",
    "# 使用盖帽方法处理极端值\n",
    "\n",
    "def block(x): \n",
    "\n",
    "    qu1 = x.quantile(.9)\n",
    "    qu2 = x.quantile(.1)\n",
    "    out = x.mask(x>qu1,qu1)#  看mask解决这个问题 \n",
    "    out = x.mask(x<qu2,qu2)\n",
    "    return out \n",
    "\n",
    "def block2(df):\n",
    "    df1 = df.copy()\n",
    "    df1['贷款与已还贷款比列']  = block(df1['贷款与已还贷款比列'])\n",
    "\n",
    "    return df1 \n",
    "\n",
    "user_info_1 = block2(user_info)\n",
    "user_info_2 = user_info_1[(user_info_1.贷款与已还贷款比列 != user_info_1.贷款与已还贷款比列.max()) & (user_info_1.贷款与已还贷款比列 > 0)]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 100994 entries, 1 to 199716\n",
      "Data columns (total 49 columns):\n",
      " #   Column          Non-Null Count   Dtype  \n",
      "---  ------          --------------   -----  \n",
      " 0   客户编号            100994 non-null  int64  \n",
      " 1   已发货款            100994 non-null  int64  \n",
      " 2   资产成本            100994 non-null  int64  \n",
      " 3   贷款与资产比列         100994 non-null  float64\n",
      " 4   品牌              100994 non-null  int64  \n",
      " 5   骑车销售商           100994 non-null  int64  \n",
      " 6   车厂              100994 non-null  int64  \n",
      " 7   出生日期            100994 non-null  int64  \n",
      " 8   货款日期            100994 non-null  int64  \n",
      " 9   地区              100994 non-null  int64  \n",
      " 10  对接员工编号          100994 non-null  int64  \n",
      " 11  是否填写手机号         100994 non-null  int64  \n",
      " 12  受否填写身份证         100994 non-null  int64  \n",
      " 13  是否出具驾驶证         100994 non-null  int64  \n",
      " 14  是否填写护照          100994 non-null  int64  \n",
      " 15  信用评分            100994 non-null  int64  \n",
      " 16  主账户贷款次数         100994 non-null  int64  \n",
      " 17  主账户有效贷款次数       100994 non-null  int64  \n",
      " 18  主账户中尚未还清有效贷款    100994 non-null  int64  \n",
      " 19  主账户中已批准的贷款      100994 non-null  int64  \n",
      " 20  主账户中已发放贷款       100994 non-null  int64  \n",
      " 21  次账户贷款次数         100994 non-null  int64  \n",
      " 22  次账户有效贷款次数       100994 non-null  int64  \n",
      " 23  次账户中尚未还清有效贷款    100994 non-null  int64  \n",
      " 24  次账户中已批准贷款       100994 non-null  int64  \n",
      " 25  次账户中已发放贷款       100994 non-null  int64  \n",
      " 26  主账户每月还款         100994 non-null  int64  \n",
      " 27  次账户没用还款         100994 non-null  int64  \n",
      " 28  近六个月新贷款次数       100994 non-null  int64  \n",
      " 29  近六个月违约次数        100994 non-null  int64  \n",
      " 30  平均贷款期限          100994 non-null  int64  \n",
      " 31  第一次贷款距今时间       100994 non-null  int64  \n",
      " 32  贷款查询次数          100994 non-null  int64  \n",
      " 33  是否违约            100994 non-null  int64  \n",
      " 34  贷款与资产比          100994 non-null  float64\n",
      " 35  贷款总次数           100994 non-null  int64  \n",
      " 36  主账户无效贷款次数       100994 non-null  int64  \n",
      " 37  次账户无效贷款次数       100994 non-null  int64  \n",
      " 38  无效贷款总次数         100994 non-null  int64  \n",
      " 39  尚未还清有效贷款总额      100994 non-null  int64  \n",
      " 40  已批准贷款总额         100994 non-null  int64  \n",
      " 41  已发放贷款总额         100994 non-null  int64  \n",
      " 42  每月还款总额          100994 non-null  int64  \n",
      " 43  贷款与已还贷款比列       100994 non-null  float64\n",
      " 44  主账户还款期数         100994 non-null  int64  \n",
      " 45  次账户还款期数         100994 non-null  int64  \n",
      " 46  贷款与已批准贷款比列      100994 non-null  float64\n",
      " 47  总贷款次数与总有效贷款次数比  100994 non-null  float64\n",
      " 48  工作类型            100994 non-null  int64  \n",
      "dtypes: float64(5), int64(44)\n",
      "memory usage: 38.5 MB\n"
     ]
    }
   ],
   "source": [
    "#查看有无缺失 ： \n",
    "user_info_2.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_info_2.工作类型.value_counts()/user_info_2.工作类型.value_counts().sum()\n",
    "# 空值占比2% 很少 直接删除 \n",
    "user_info_worktype = user_info_2.loc[(user_info_2.工作类型 != 2)] # 尝试去除工作类型为空的记录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    98838.000000\n",
       "mean       577.227645\n",
       "std        250.891976\n",
       "min          0.000000\n",
       "25%        471.000000\n",
       "50%        679.000000\n",
       "75%        738.000000\n",
       "max        890.000000\n",
       "Name: 信用评分, dtype: float64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info_worktype['信用评分'].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_info_worktype[user_info_worktype.信用评分 == 0].count()/user_info_worktype.count()\n",
    "\n",
    "# 信用评分空值占比 0.97% 量很少, 删 \n",
    "\n",
    "user_info_worktype2 = user_info_worktype[user_info_worktype.信用评分 != 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>index</th>\n",
       "      <th>客户编号</th>\n",
       "      <th>已发货款</th>\n",
       "      <th>资产成本</th>\n",
       "      <th>贷款与资产比列</th>\n",
       "      <th>品牌</th>\n",
       "      <th>骑车销售商</th>\n",
       "      <th>车厂</th>\n",
       "      <th>出生日期</th>\n",
       "      <th>货款日期</th>\n",
       "      <th>...</th>\n",
       "      <th>尚未还清有效贷款总额</th>\n",
       "      <th>已批准贷款总额</th>\n",
       "      <th>已发放贷款总额</th>\n",
       "      <th>每月还款总额</th>\n",
       "      <th>贷款与已还贷款比列</th>\n",
       "      <th>主账户还款期数</th>\n",
       "      <th>次账户还款期数</th>\n",
       "      <th>贷款与已批准贷款比列</th>\n",
       "      <th>总贷款次数与总有效贷款次数比</th>\n",
       "      <th>工作类型</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>519488</td>\n",
       "      <td>56759</td>\n",
       "      <td>65325</td>\n",
       "      <td>89.55</td>\n",
       "      <td>61</td>\n",
       "      <td>22778</td>\n",
       "      <td>86</td>\n",
       "      <td>1967</td>\n",
       "      <td>2018</td>\n",
       "      <td>...</td>\n",
       "      <td>2054139</td>\n",
       "      <td>2036500</td>\n",
       "      <td>2036500</td>\n",
       "      <td>34455</td>\n",
       "      <td>1.00</td>\n",
       "      <td>59</td>\n",
       "      <td>0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.33</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>648134</td>\n",
       "      <td>72317</td>\n",
       "      <td>99750</td>\n",
       "      <td>73.68</td>\n",
       "      <td>76</td>\n",
       "      <td>17242</td>\n",
       "      <td>48</td>\n",
       "      <td>1995</td>\n",
       "      <td>2018</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>13813</td>\n",
       "      <td>13813</td>\n",
       "      <td>0</td>\n",
       "      <td>13814.00</td>\n",
       "      <td>13813</td>\n",
       "      <td>0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>2.00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4</td>\n",
       "      <td>458210</td>\n",
       "      <td>50078</td>\n",
       "      <td>65450</td>\n",
       "      <td>79.45</td>\n",
       "      <td>146</td>\n",
       "      <td>14181</td>\n",
       "      <td>45</td>\n",
       "      <td>1974</td>\n",
       "      <td>2018</td>\n",
       "      <td>...</td>\n",
       "      <td>467161</td>\n",
       "      <td>550000</td>\n",
       "      <td>550000</td>\n",
       "      <td>12863</td>\n",
       "      <td>1.18</td>\n",
       "      <td>42</td>\n",
       "      <td>0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.06</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5</td>\n",
       "      <td>616513</td>\n",
       "      <td>63882</td>\n",
       "      <td>79605</td>\n",
       "      <td>82.91</td>\n",
       "      <td>152</td>\n",
       "      <td>14470</td>\n",
       "      <td>51</td>\n",
       "      <td>1993</td>\n",
       "      <td>2018</td>\n",
       "      <td>...</td>\n",
       "      <td>16225</td>\n",
       "      <td>17700</td>\n",
       "      <td>17700</td>\n",
       "      <td>1475</td>\n",
       "      <td>1.09</td>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.50</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>6</td>\n",
       "      <td>453368</td>\n",
       "      <td>54013</td>\n",
       "      <td>62371</td>\n",
       "      <td>89.79</td>\n",
       "      <td>34</td>\n",
       "      <td>16556</td>\n",
       "      <td>86</td>\n",
       "      <td>1971</td>\n",
       "      <td>2018</td>\n",
       "      <td>...</td>\n",
       "      <td>12991</td>\n",
       "      <td>100000</td>\n",
       "      <td>100000</td>\n",
       "      <td>3207</td>\n",
       "      <td>7.70</td>\n",
       "      <td>31</td>\n",
       "      <td>0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.33</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97879</th>\n",
       "      <td>199707</td>\n",
       "      <td>482490</td>\n",
       "      <td>55413</td>\n",
       "      <td>69086</td>\n",
       "      <td>82.51</td>\n",
       "      <td>67</td>\n",
       "      <td>21308</td>\n",
       "      <td>86</td>\n",
       "      <td>1992</td>\n",
       "      <td>2018</td>\n",
       "      <td>...</td>\n",
       "      <td>400400</td>\n",
       "      <td>403000</td>\n",
       "      <td>403000</td>\n",
       "      <td>4579</td>\n",
       "      <td>1.01</td>\n",
       "      <td>87</td>\n",
       "      <td>0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>2.00</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97880</th>\n",
       "      <td>199709</td>\n",
       "      <td>470368</td>\n",
       "      <td>52199</td>\n",
       "      <td>63387</td>\n",
       "      <td>88.35</td>\n",
       "      <td>101</td>\n",
       "      <td>24379</td>\n",
       "      <td>86</td>\n",
       "      <td>1985</td>\n",
       "      <td>2018</td>\n",
       "      <td>...</td>\n",
       "      <td>72033</td>\n",
       "      <td>75000</td>\n",
       "      <td>80288</td>\n",
       "      <td>5354</td>\n",
       "      <td>1.11</td>\n",
       "      <td>14</td>\n",
       "      <td>0</td>\n",
       "      <td>1.07</td>\n",
       "      <td>1.75</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97881</th>\n",
       "      <td>199711</td>\n",
       "      <td>432468</td>\n",
       "      <td>63447</td>\n",
       "      <td>73701</td>\n",
       "      <td>88.19</td>\n",
       "      <td>13</td>\n",
       "      <td>14614</td>\n",
       "      <td>86</td>\n",
       "      <td>1976</td>\n",
       "      <td>2018</td>\n",
       "      <td>...</td>\n",
       "      <td>128709</td>\n",
       "      <td>214103</td>\n",
       "      <td>214103</td>\n",
       "      <td>354750</td>\n",
       "      <td>1.66</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.44</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97882</th>\n",
       "      <td>199712</td>\n",
       "      <td>436304</td>\n",
       "      <td>36439</td>\n",
       "      <td>60424</td>\n",
       "      <td>62.89</td>\n",
       "      <td>10</td>\n",
       "      <td>23507</td>\n",
       "      <td>45</td>\n",
       "      <td>1986</td>\n",
       "      <td>2018</td>\n",
       "      <td>...</td>\n",
       "      <td>592668</td>\n",
       "      <td>525000</td>\n",
       "      <td>525000</td>\n",
       "      <td>0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>525000</td>\n",
       "      <td>0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>3.00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97883</th>\n",
       "      <td>199714</td>\n",
       "      <td>466468</td>\n",
       "      <td>54413</td>\n",
       "      <td>62710</td>\n",
       "      <td>89.30</td>\n",
       "      <td>67</td>\n",
       "      <td>16565</td>\n",
       "      <td>45</td>\n",
       "      <td>1973</td>\n",
       "      <td>2018</td>\n",
       "      <td>...</td>\n",
       "      <td>1185601</td>\n",
       "      <td>1220000</td>\n",
       "      <td>1220000</td>\n",
       "      <td>2500</td>\n",
       "      <td>1.03</td>\n",
       "      <td>487</td>\n",
       "      <td>0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>3.00</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>97884 rows × 50 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        index    客户编号   已发货款   资产成本  贷款与资产比列   品牌  骑车销售商  车厂  出生日期  货款日期  ...  \\\n",
       "0           1  519488  56759  65325    89.55   61  22778  86  1967  2018  ...   \n",
       "1           3  648134  72317  99750    73.68   76  17242  48  1995  2018  ...   \n",
       "2           4  458210  50078  65450    79.45  146  14181  45  1974  2018  ...   \n",
       "3           5  616513  63882  79605    82.91  152  14470  51  1993  2018  ...   \n",
       "4           6  453368  54013  62371    89.79   34  16556  86  1971  2018  ...   \n",
       "...       ...     ...    ...    ...      ...  ...    ...  ..   ...   ...  ...   \n",
       "97879  199707  482490  55413  69086    82.51   67  21308  86  1992  2018  ...   \n",
       "97880  199709  470368  52199  63387    88.35  101  24379  86  1985  2018  ...   \n",
       "97881  199711  432468  63447  73701    88.19   13  14614  86  1976  2018  ...   \n",
       "97882  199712  436304  36439  60424    62.89   10  23507  45  1986  2018  ...   \n",
       "97883  199714  466468  54413  62710    89.30   67  16565  45  1973  2018  ...   \n",
       "\n",
       "       尚未还清有效贷款总额  已批准贷款总额  已发放贷款总额  每月还款总额  贷款与已还贷款比列  主账户还款期数  次账户还款期数  \\\n",
       "0         2054139  2036500  2036500   34455       1.00       59        0   \n",
       "1               0    13813    13813       0   13814.00    13813        0   \n",
       "2          467161   550000   550000   12863       1.18       42        0   \n",
       "3           16225    17700    17700    1475       1.09       11        0   \n",
       "4           12991   100000   100000    3207       7.70       31        0   \n",
       "...           ...      ...      ...     ...        ...      ...      ...   \n",
       "97879      400400   403000   403000    4579       1.01       87        0   \n",
       "97880       72033    75000    80288    5354       1.11       14        0   \n",
       "97881      128709   214103   214103  354750       1.66        0        0   \n",
       "97882      592668   525000   525000       0       1.00   525000        0   \n",
       "97883     1185601  1220000  1220000    2500       1.03      487        0   \n",
       "\n",
       "       贷款与已批准贷款比列  总贷款次数与总有效贷款次数比  工作类型  \n",
       "0            1.00            1.33     1  \n",
       "1            1.00            2.00     0  \n",
       "2            1.00            1.06     1  \n",
       "3            1.00            1.50     0  \n",
       "4            1.00            1.33     1  \n",
       "...           ...             ...   ...  \n",
       "97879        1.00            2.00     1  \n",
       "97880        1.07            1.75     0  \n",
       "97881        1.00            1.44     1  \n",
       "97882        1.00            3.00     0  \n",
       "97883        1.00            3.00     1  \n",
       "\n",
       "[97884 rows x 50 columns]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info_worktype2.reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_info_worktype3 = user_info_worktype2[user_info_worktype2.尚未还清有效贷款总额 >= 0 ]\n",
    "user_info_worktype3 = user_info_worktype3[user_info_worktype3.贷款与已还贷款比列 <=200]\n",
    "\n",
    "user_info_worktype3.reset_index(inplace = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>index</th>\n",
       "      <th>客户编号</th>\n",
       "      <th>已发货款</th>\n",
       "      <th>资产成本</th>\n",
       "      <th>贷款与资产比列</th>\n",
       "      <th>品牌</th>\n",
       "      <th>骑车销售商</th>\n",
       "      <th>车厂</th>\n",
       "      <th>出生日期</th>\n",
       "      <th>货款日期</th>\n",
       "      <th>...</th>\n",
       "      <th>尚未还清有效贷款总额</th>\n",
       "      <th>已批准贷款总额</th>\n",
       "      <th>已发放贷款总额</th>\n",
       "      <th>每月还款总额</th>\n",
       "      <th>贷款与已还贷款比列</th>\n",
       "      <th>主账户还款期数</th>\n",
       "      <th>次账户还款期数</th>\n",
       "      <th>贷款与已批准贷款比列</th>\n",
       "      <th>总贷款次数与总有效贷款次数比</th>\n",
       "      <th>工作类型</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>93936.000000</td>\n",
       "      <td>93936.000000</td>\n",
       "      <td>93936.000000</td>\n",
       "      <td>9.393600e+04</td>\n",
       "      <td>93936.000000</td>\n",
       "      <td>93936.000000</td>\n",
       "      <td>93936.000000</td>\n",
       "      <td>93936.000000</td>\n",
       "      <td>93936.000000</td>\n",
       "      <td>93936.0</td>\n",
       "      <td>...</td>\n",
       "      <td>9.393600e+04</td>\n",
       "      <td>9.393600e+04</td>\n",
       "      <td>9.393600e+04</td>\n",
       "      <td>9.393600e+04</td>\n",
       "      <td>93936.000000</td>\n",
       "      <td>9.393600e+04</td>\n",
       "      <td>9.393600e+04</td>\n",
       "      <td>9.393600e+04</td>\n",
       "      <td>93936.000000</td>\n",
       "      <td>93936.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>101121.418476</td>\n",
       "      <td>534895.085718</td>\n",
       "      <td>54495.729039</td>\n",
       "      <td>7.475921e+04</td>\n",
       "      <td>75.903667</td>\n",
       "      <td>71.710409</td>\n",
       "      <td>19344.164112</td>\n",
       "      <td>70.842584</td>\n",
       "      <td>1981.717744</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>...</td>\n",
       "      <td>3.624342e+05</td>\n",
       "      <td>4.633436e+05</td>\n",
       "      <td>4.624228e+05</td>\n",
       "      <td>2.697798e+04</td>\n",
       "      <td>2.599336</td>\n",
       "      <td>9.337971e+04</td>\n",
       "      <td>5.170499e+03</td>\n",
       "      <td>1.164677e+03</td>\n",
       "      <td>1.876879</td>\n",
       "      <td>0.452127</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>57161.335169</td>\n",
       "      <td>68243.648077</td>\n",
       "      <td>13102.984279</td>\n",
       "      <td>1.870832e+04</td>\n",
       "      <td>11.151035</td>\n",
       "      <td>67.333622</td>\n",
       "      <td>3518.321027</td>\n",
       "      <td>22.236936</td>\n",
       "      <td>9.418742</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>1.394011e+06</td>\n",
       "      <td>1.679415e+06</td>\n",
       "      <td>1.689696e+06</td>\n",
       "      <td>2.153531e+05</td>\n",
       "      <td>7.383551</td>\n",
       "      <td>5.973408e+05</td>\n",
       "      <td>1.505532e+05</td>\n",
       "      <td>1.664141e+05</td>\n",
       "      <td>0.949783</td>\n",
       "      <td>0.497706</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>417428.000000</td>\n",
       "      <td>13652.000000</td>\n",
       "      <td>3.700000e+04</td>\n",
       "      <td>13.500000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>10524.000000</td>\n",
       "      <td>45.000000</td>\n",
       "      <td>1954.000000</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>52101.000000</td>\n",
       "      <td>476322.750000</td>\n",
       "      <td>47349.000000</td>\n",
       "      <td>6.531950e+04</td>\n",
       "      <td>70.120000</td>\n",
       "      <td>14.000000</td>\n",
       "      <td>16120.000000</td>\n",
       "      <td>48.000000</td>\n",
       "      <td>1975.000000</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>...</td>\n",
       "      <td>5.155250e+03</td>\n",
       "      <td>1.500000e+04</td>\n",
       "      <td>1.449000e+04</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>3.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>1.250000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>101369.000000</td>\n",
       "      <td>533961.000000</td>\n",
       "      <td>53803.500000</td>\n",
       "      <td>7.013100e+04</td>\n",
       "      <td>78.240000</td>\n",
       "      <td>63.000000</td>\n",
       "      <td>18532.000000</td>\n",
       "      <td>86.000000</td>\n",
       "      <td>1983.000000</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>...</td>\n",
       "      <td>4.200000e+04</td>\n",
       "      <td>7.000000e+04</td>\n",
       "      <td>6.886050e+04</td>\n",
       "      <td>2.160000e+03</td>\n",
       "      <td>1.230000</td>\n",
       "      <td>2.300000e+01</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>1.670000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>150521.250000</td>\n",
       "      <td>593452.750000</td>\n",
       "      <td>60289.500000</td>\n",
       "      <td>7.753225e+04</td>\n",
       "      <td>84.530000</td>\n",
       "      <td>135.000000</td>\n",
       "      <td>22892.000000</td>\n",
       "      <td>86.000000</td>\n",
       "      <td>1989.000000</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>...</td>\n",
       "      <td>2.318732e+05</td>\n",
       "      <td>3.500000e+05</td>\n",
       "      <td>3.484005e+05</td>\n",
       "      <td>8.994000e+03</td>\n",
       "      <td>1.920000</td>\n",
       "      <td>9.600500e+03</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>199714.000000</td>\n",
       "      <td>671033.000000</td>\n",
       "      <td>987354.000000</td>\n",
       "      <td>1.328954e+06</td>\n",
       "      <td>95.000000</td>\n",
       "      <td>261.000000</td>\n",
       "      <td>24803.000000</td>\n",
       "      <td>156.000000</td>\n",
       "      <td>1997.000000</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>...</td>\n",
       "      <td>9.652492e+07</td>\n",
       "      <td>1.058657e+08</td>\n",
       "      <td>1.057557e+08</td>\n",
       "      <td>2.564281e+07</td>\n",
       "      <td>199.000000</td>\n",
       "      <td>5.326646e+07</td>\n",
       "      <td>1.980000e+07</td>\n",
       "      <td>5.000000e+07</td>\n",
       "      <td>18.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8 rows × 50 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "               index           客户编号           已发货款          资产成本  \\\n",
       "count   93936.000000   93936.000000   93936.000000  9.393600e+04   \n",
       "mean   101121.418476  534895.085718   54495.729039  7.475921e+04   \n",
       "std     57161.335169   68243.648077   13102.984279  1.870832e+04   \n",
       "min         1.000000  417428.000000   13652.000000  3.700000e+04   \n",
       "25%     52101.000000  476322.750000   47349.000000  6.531950e+04   \n",
       "50%    101369.000000  533961.000000   53803.500000  7.013100e+04   \n",
       "75%    150521.250000  593452.750000   60289.500000  7.753225e+04   \n",
       "max    199714.000000  671033.000000  987354.000000  1.328954e+06   \n",
       "\n",
       "            贷款与资产比列            品牌         骑车销售商            车厂          出生日期  \\\n",
       "count  93936.000000  93936.000000  93936.000000  93936.000000  93936.000000   \n",
       "mean      75.903667     71.710409  19344.164112     70.842584   1981.717744   \n",
       "std       11.151035     67.333622   3518.321027     22.236936      9.418742   \n",
       "min       13.500000      1.000000  10524.000000     45.000000   1954.000000   \n",
       "25%       70.120000     14.000000  16120.000000     48.000000   1975.000000   \n",
       "50%       78.240000     63.000000  18532.000000     86.000000   1983.000000   \n",
       "75%       84.530000    135.000000  22892.000000     86.000000   1989.000000   \n",
       "max       95.000000    261.000000  24803.000000    156.000000   1997.000000   \n",
       "\n",
       "          货款日期  ...    尚未还清有效贷款总额       已批准贷款总额       已发放贷款总额        每月还款总额  \\\n",
       "count  93936.0  ...  9.393600e+04  9.393600e+04  9.393600e+04  9.393600e+04   \n",
       "mean    2018.0  ...  3.624342e+05  4.633436e+05  4.624228e+05  2.697798e+04   \n",
       "std        0.0  ...  1.394011e+06  1.679415e+06  1.689696e+06  2.153531e+05   \n",
       "min     2018.0  ...  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00   \n",
       "25%     2018.0  ...  5.155250e+03  1.500000e+04  1.449000e+04  0.000000e+00   \n",
       "50%     2018.0  ...  4.200000e+04  7.000000e+04  6.886050e+04  2.160000e+03   \n",
       "75%     2018.0  ...  2.318732e+05  3.500000e+05  3.484005e+05  8.994000e+03   \n",
       "max     2018.0  ...  9.652492e+07  1.058657e+08  1.057557e+08  2.564281e+07   \n",
       "\n",
       "          贷款与已还贷款比列       主账户还款期数       次账户还款期数    贷款与已批准贷款比列  总贷款次数与总有效贷款次数比  \\\n",
       "count  93936.000000  9.393600e+04  9.393600e+04  9.393600e+04    93936.000000   \n",
       "mean       2.599336  9.337971e+04  5.170499e+03  1.164677e+03        1.876879   \n",
       "std        7.383551  5.973408e+05  1.505532e+05  1.664141e+05        0.949783   \n",
       "min        1.000000  0.000000e+00  0.000000e+00  0.000000e+00        1.000000   \n",
       "25%        1.000000  3.000000e+00  0.000000e+00  1.000000e+00        1.250000   \n",
       "50%        1.230000  2.300000e+01  0.000000e+00  1.000000e+00        1.670000   \n",
       "75%        1.920000  9.600500e+03  0.000000e+00  1.000000e+00        2.000000   \n",
       "max      199.000000  5.326646e+07  1.980000e+07  5.000000e+07       18.000000   \n",
       "\n",
       "               工作类型  \n",
       "count  93936.000000  \n",
       "mean       0.452127  \n",
       "std        0.497706  \n",
       "min        0.000000  \n",
       "25%        0.000000  \n",
       "50%        0.000000  \n",
       "75%        1.000000  \n",
       "max        1.000000  \n",
       "\n",
       "[8 rows x 50 columns]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info_worktype3.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##  衍生字段"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 年龄分段\n",
    "\n",
    "def age_t(df): \n",
    "    ages = df['货款日期'] - df['出生日期']\n",
    "    age  = []\n",
    "    # 大学生段\n",
    "    for i in range(len(ages)): \n",
    "        if ages[i] <= 23 :  \n",
    "            age.append(1) \n",
    "        #刚工作\n",
    "        elif ages[i] <= 26:\n",
    "            age.append(2) \n",
    "        #稳定期\n",
    "        elif ages[i] <= 30:\n",
    "            age.append(3)\n",
    "\n",
    "        elif ages[i] <= 40:\n",
    "            age.append(4)\n",
    "\n",
    "        elif ages[i] <= 50:\n",
    "            age.append(5)\n",
    "\n",
    "        else:\n",
    "            age.append(6)\n",
    "    agex = pd.DataFrame(age)\n",
    "    return agex \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 发现一个问题，没没贷款的人还有违约的标签，数据异常，删\n",
    "user_info_worktype2.reset_index(inplace = True)\n",
    "index = pd.Series([not x for x in (user_info_worktype2['是否违约']== 1) &(user_info_worktype2['贷款总次数']==0)]) \n",
    "user_info_worktype3 = user_info_worktype2[index]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_set = pd.DataFrame()\n",
    "train_set['是否违约'] = user_info_worktype3['是否违约']\n",
    "train_set['信用评分'] = user_info_worktype3['信用评分']\n",
    "train_set['工作类型'] = user_info_worktype3['工作类型']\n",
    "train_set['是否出具驾驶证'] = user_info_worktype3['是否出具驾驶证']\n",
    "train_set['是否填写护照'] = user_info_worktype3['是否填写护照']\n",
    "train_set['年岭区间'] = age_t(user_info_worktype3)\n",
    "train_set['总贷款次数'] = user_info_worktype3['主账户贷款次数']+user_info_worktype3['次账户贷款次数']\n",
    "train_set['总有效贷款次数'] = user_info_worktype3['主账户有效贷款次数']+user_info_worktype3['次账户有效贷款次数']\n",
    "train_set['贷款成功率'] = round(train_set['总有效贷款次数']/train_set['总贷款次数'],4)\n",
    "train_set['用户资产'] = (user_info_worktype3['尚未还清有效贷款总额'] / (1 - 1/user_info_worktype3['贷款与已还贷款比列']))/user_info_worktype3['贷款与资产比']\n",
    "train_set['总还款期数'] = user_info_worktype3['主账户还款期数']+user_info_worktype3['次账户贷款次数']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "import pandas as pd\n",
      "import numpy as np\n",
      "import matplotlib.pyplot as plt\n",
      "from scipy import stats\n",
      "user_info = pd.read_csv(r\"./车贷违约预测.csv\",encoding = 'GBK')\n",
      "user_info.head()\n",
      "# 统计类描述\n",
      "user_info.describe().iloc[:,:10]\n",
      "user_info.describe().iloc[:,10:20]\n",
      "user_info.describe().iloc[:,20:30]\n",
      "user_info.describe().iloc[:,30:40]\n",
      "user_info.describe().iloc[:,40:]\n",
      "# 数据有效性 没贷款的人没有预测价值 谈不上违约\n",
      "user_info = user_info[user_info['贷款总次数'] != 0]\n",
      "\n",
      "#查看有无重复 ： 无\n",
      "user_info.duplicated().sum()\n",
      "\n",
      "# 使用盖帽方法处理极端值\n",
      "\n",
      "def block(x): \n",
      "\n",
      "    qu1 = x.quantile(.9)\n",
      "    qu2 = x.quantile(.1)\n",
      "    out = x.mask(x>qu1,qu1)#  看mask解决这个问题 \n",
      "    out = x.mask(x<qu2,qu2)\n",
      "    return out \n",
      "\n",
      "def block2(df):\n",
      "    df1 = df.copy()\n",
      "    df1['贷款与已还贷款比列']  = block(df1['贷款与已还贷款比列'])\n",
      "\n",
      "    return df1 \n",
      "\n",
      "user_info_1 = block2(user_info)\n",
      "user_info_2 = user_info_1[(user_info_1.贷款与已还贷款比列 != user_info_1.贷款与已还贷款比列.max()) & (user_info_1.贷款与已还贷款比列 > 0)]\n",
      "#查看有无缺失 ： \n",
      "user_info_2.info()\n",
      "user_info_2.工作类型.value_counts()/user_info_2.工作类型.value_counts().sum()\n",
      "# 空值占比2% 很少 直接删除 \n",
      "user_info_worktype = user_info_2.loc[(user_info_2.工作类型 != 2)] # 尝试去除工作类型为空的记录\n",
      "user_info_worktype['信用评分'].describe()\n",
      "user_info_worktype[user_info_worktype.信用评分 == 0].count()/user_info_worktype.count()\n",
      "\n",
      "# 信用评分空值占比 0.97% 量很少, 删 \n",
      "\n",
      "user_info_worktype2 = user_info_worktype[user_info_worktype.信用评分 != 0]\n",
      "user_info_worktype2.reset_index()\n",
      "user_info_worktype3 = user_info_worktype2[user_info_worktype2.尚未还清有效贷款总额 >= 0 ]\n",
      "user_info_worktype3 = user_info_worktype3[user_info_worktype3.贷款与已还贷款比列 <=200]\n",
      "\n",
      "user_info_worktype3.reset_index(inplace = True)\n",
      "user_info_worktype3.describe()\n",
      "# 年龄分段\n",
      "\n",
      "def age_t(df): \n",
      "    ages = df['货款日期'] - df['出生日期']\n",
      "    age  = []\n",
      "    # 大学生段\n",
      "    for i in range(len(ages)): \n",
      "        if ages[i] <= 23 :  \n",
      "            age.append(1) \n",
      "        #刚工作\n",
      "        elif ages[i] <= 26:\n",
      "            age.append(2) \n",
      "        #稳定期\n",
      "        elif ages[i] <= 30:\n",
      "            age.append(3)\n",
      "\n",
      "        elif ages[i] <= 40:\n",
      "            age.append(4)\n",
      "\n",
      "        elif ages[i] <= 50:\n",
      "            age.append(5)\n",
      "\n",
      "        else:\n",
      "            age.append(6)\n",
      "    agex = pd.DataFrame(age)\n",
      "    return agex\n",
      "# 发现一个问题，没没贷款的人还有违约的标签，数据异常，删\n",
      "user_info_worktype2.reset_index(inplace = True)\n",
      "index = pd.Series([not x for x in (user_info_worktype2['是否违约']== 1) &(user_info_worktype2['贷款总次数']==0)]) \n",
      "user_info_worktype3 = user_info_worktype2[index]\n",
      "train_set = pd.DataFrame()\n",
      "train_set['是否违约'] = user_info_worktype3['是否违约']\n",
      "train_set['信用评分'] = user_info_worktype3['信用评分']\n",
      "train_set['工作类型'] = user_info_worktype3['工作类型']\n",
      "train_set['是否出具驾驶证'] = user_info_worktype3['是否出具驾驶证']\n",
      "train_set['是否填写护照'] = user_info_worktype3['是否填写护照']\n",
      "train_set['年岭区间'] = age_t(user_info_worktype3)\n",
      "train_set['总贷款次数'] = user_info_worktype3['主账户贷款次数']+user_info_worktype3['次账户贷款次数']\n",
      "train_set['总有效贷款次数'] = user_info_worktype3['主账户有效贷款次数']+user_info_worktype3['次账户有效贷款次数']\n",
      "train_set['贷款成功率'] = round(train_set['总有效贷款次数']/train_set['总贷款次数'],4)\n",
      "train_set['用户资产'] = (user_info_worktype3['尚未还清有效贷款总额'] / (1 - 1/user_info_worktype3['贷款与已还贷款比列']))/user_info_worktype3['贷款与资产比']\n",
      "train_set['总还款期数'] = user_info_worktype3['主账户还款期数']+user_info_worktype3['次账户贷款次数']\n",
      "history\n"
     ]
    }
   ],
   "source": [
    "history"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>是否违约</th>\n",
       "      <th>信用评分</th>\n",
       "      <th>工作类型</th>\n",
       "      <th>是否出具驾驶证</th>\n",
       "      <th>是否填写护照</th>\n",
       "      <th>年岭区间</th>\n",
       "      <th>总贷款次数</th>\n",
       "      <th>总有效贷款次数</th>\n",
       "      <th>贷款成功率</th>\n",
       "      <th>用户资产</th>\n",
       "      <th>总还款期数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>300</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "      <td>0.2857</td>\n",
       "      <td>inf</td>\n",
       "      <td>59</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>763</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>13813</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>379</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>16</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0625</td>\n",
       "      <td>4.002568e+06</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>749</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0.5000</td>\n",
       "      <td>2.448672e+05</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>300</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0.3333</td>\n",
       "      <td>1.724022e+04</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97879</th>\n",
       "      <td>0</td>\n",
       "      <td>771</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0000</td>\n",
       "      <td>5.041895e+07</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97880</th>\n",
       "      <td>0</td>\n",
       "      <td>300</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>0.5000</td>\n",
       "      <td>8.826729e+05</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97881</th>\n",
       "      <td>0</td>\n",
       "      <td>726</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>22</td>\n",
       "      <td>7</td>\n",
       "      <td>0.3182</td>\n",
       "      <td>3.760411e+05</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97882</th>\n",
       "      <td>0</td>\n",
       "      <td>753</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0000</td>\n",
       "      <td>inf</td>\n",
       "      <td>525000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97883</th>\n",
       "      <td>0</td>\n",
       "      <td>771</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0000</td>\n",
       "      <td>4.691251e+07</td>\n",
       "      <td>487</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>97884 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       是否违约  信用评分  工作类型  是否出具驾驶证  是否填写护照  年岭区间  总贷款次数  总有效贷款次数   贷款成功率  \\\n",
       "0         1   300     1        0       0     6      7        2  0.2857   \n",
       "1         1   763     0        0       0     1      1        1  1.0000   \n",
       "2         1   379     1        0       0     5     16        1  0.0625   \n",
       "3         1   749     0        0       0     2      2        1  0.5000   \n",
       "4         1   300     1        0       0     5      3        1  0.3333   \n",
       "...     ...   ...   ...      ...     ...   ...    ...      ...     ...   \n",
       "97879     0   771     1        0       0     2      1        1  1.0000   \n",
       "97880     0   300     0        0       0     4      6        3  0.5000   \n",
       "97881     0   726     1        0       0     5     22        7  0.3182   \n",
       "97882     0   753     0        0       0     4      2        2  1.0000   \n",
       "97883     0   771     1        0       0     5      2        2  1.0000   \n",
       "\n",
       "               用户资产   总还款期数  \n",
       "0               inf      59  \n",
       "1      0.000000e+00   13813  \n",
       "2      4.002568e+06      50  \n",
       "3      2.448672e+05      11  \n",
       "4      1.724022e+04      31  \n",
       "...             ...     ...  \n",
       "97879  5.041895e+07      87  \n",
       "97880  8.826729e+05      14  \n",
       "97881  3.760411e+05       0  \n",
       "97882           inf  525000  \n",
       "97883  4.691251e+07     487  \n",
       "\n",
       "[97884 rows x 11 columns]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_set"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 失衡处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Dell\\AppData\\Local\\Temp/ipykernel_6960/583083778.py:3: FutureWarning: In a future version of pandas all arguments of DataFrame.drop except for the argument 'labels' will be keyword-only\n",
      "  ts = train_set.drop('是否违约',1)\n",
      "C:\\Users\\Dell\\AppData\\Local\\Temp/ipykernel_6960/583083778.py:4: FutureWarning: In a future version of pandas all arguments of DataFrame.drop except for the argument 'labels' will be keyword-only\n",
      "  ts2 = user_info_worktype3.drop('是否违约',1)\n"
     ]
    }
   ],
   "source": [
    "del train_set['用户资产']\n",
    "\n",
    "ts = train_set.drop('是否违约',1)\n",
    "ts2 = user_info_worktype3.drop('是否违约',1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>信用评分</th>\n",
       "      <th>工作类型</th>\n",
       "      <th>是否出具驾驶证</th>\n",
       "      <th>是否填写护照</th>\n",
       "      <th>年岭区间</th>\n",
       "      <th>总贷款次数</th>\n",
       "      <th>总有效贷款次数</th>\n",
       "      <th>贷款成功率</th>\n",
       "      <th>总还款期数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>300</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "      <td>0.2857</td>\n",
       "      <td>59</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>763</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0000</td>\n",
       "      <td>13813</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>379</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>16</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0625</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>749</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0.5000</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>300</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0.3333</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97879</th>\n",
       "      <td>771</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0000</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97880</th>\n",
       "      <td>300</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>0.5000</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97881</th>\n",
       "      <td>726</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>22</td>\n",
       "      <td>7</td>\n",
       "      <td>0.3182</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97882</th>\n",
       "      <td>753</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0000</td>\n",
       "      <td>525000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97883</th>\n",
       "      <td>771</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0000</td>\n",
       "      <td>487</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>97884 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       信用评分  工作类型  是否出具驾驶证  是否填写护照  年岭区间  总贷款次数  总有效贷款次数   贷款成功率   总还款期数\n",
       "0       300     1        0       0     6      7        2  0.2857      59\n",
       "1       763     0        0       0     1      1        1  1.0000   13813\n",
       "2       379     1        0       0     5     16        1  0.0625      50\n",
       "3       749     0        0       0     2      2        1  0.5000      11\n",
       "4       300     1        0       0     5      3        1  0.3333      31\n",
       "...     ...   ...      ...     ...   ...    ...      ...     ...     ...\n",
       "97879   771     1        0       0     2      1        1  1.0000      87\n",
       "97880   300     0        0       0     4      6        3  0.5000      14\n",
       "97881   726     1        0       0     5     22        7  0.3182       0\n",
       "97882   753     0        0       0     4      2        2  1.0000  525000\n",
       "97883   771     1        0       0     5      2        2  1.0000     487\n",
       "\n",
       "[97884 rows x 9 columns]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "是否违约       False\n",
      "信用评分       False\n",
      "工作类型       False\n",
      "是否出具驾驶证    False\n",
      "是否填写护照     False\n",
      "年岭区间       False\n",
      "总贷款次数      False\n",
      "总有效贷款次数    False\n",
      "贷款成功率      False\n",
      "总还款期数      False\n",
      "dtype: bool\n"
     ]
    }
   ],
   "source": [
    "# Flase:表示对应特征的特征值中无缺失值  True：表示有缺失值\n",
    "print(np.isnan(train_set).any())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "信用评分       False\n",
      "工作类型       False\n",
      "是否出具驾驶证    False\n",
      "是否填写护照     False\n",
      "年岭区间       False\n",
      "总贷款次数      False\n",
      "总有效贷款次数    False\n",
      "贷款成功率      False\n",
      "总还款期数      False\n",
      "dtype: bool\n"
     ]
    }
   ],
   "source": [
    "# Flase:表示对应特征的特征值中无缺失值  True：表示有缺失值\n",
    "print(np.isnan(ts).any())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "index             False\n",
      "客户编号              False\n",
      "已发货款              False\n",
      "资产成本              False\n",
      "贷款与资产比列           False\n",
      "品牌                False\n",
      "骑车销售商             False\n",
      "车厂                False\n",
      "出生日期              False\n",
      "货款日期              False\n",
      "地区                False\n",
      "对接员工编号            False\n",
      "是否填写手机号           False\n",
      "受否填写身份证           False\n",
      "是否出具驾驶证           False\n",
      "是否填写护照            False\n",
      "信用评分              False\n",
      "主账户贷款次数           False\n",
      "主账户有效贷款次数         False\n",
      "主账户中尚未还清有效贷款      False\n",
      "主账户中已批准的贷款        False\n",
      "主账户中已发放贷款         False\n",
      "次账户贷款次数           False\n",
      "次账户有效贷款次数         False\n",
      "次账户中尚未还清有效贷款      False\n",
      "次账户中已批准贷款         False\n",
      "次账户中已发放贷款         False\n",
      "主账户每月还款           False\n",
      "次账户没用还款           False\n",
      "近六个月新贷款次数         False\n",
      "近六个月违约次数          False\n",
      "平均贷款期限            False\n",
      "第一次贷款距今时间         False\n",
      "贷款查询次数            False\n",
      "贷款与资产比            False\n",
      "贷款总次数             False\n",
      "主账户无效贷款次数         False\n",
      "次账户无效贷款次数         False\n",
      "无效贷款总次数           False\n",
      "尚未还清有效贷款总额        False\n",
      "已批准贷款总额           False\n",
      "已发放贷款总额           False\n",
      "每月还款总额            False\n",
      "贷款与已还贷款比列         False\n",
      "主账户还款期数           False\n",
      "次账户还款期数           False\n",
      "贷款与已批准贷款比列        False\n",
      "总贷款次数与总有效贷款次数比    False\n",
      "工作类型              False\n",
      "dtype: bool\n"
     ]
    }
   ],
   "source": [
    "# Flase:表示对应特征的特征值中无缺失值  True：表示有缺失值\n",
    "print(np.isnan(ts2).any())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>信用评分</th>\n",
       "      <th>工作类型</th>\n",
       "      <th>是否出具驾驶证</th>\n",
       "      <th>是否填写护照</th>\n",
       "      <th>年岭区间</th>\n",
       "      <th>总贷款次数</th>\n",
       "      <th>总有效贷款次数</th>\n",
       "      <th>贷款成功率</th>\n",
       "      <th>总还款期数</th>\n",
       "      <th>是否违约</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>738</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>836</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>825</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>16</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>825</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   信用评分  工作类型  是否出具驾驶证  是否填写护照  年岭区间  总贷款次数  总有效贷款次数  贷款成功率  总还款期数  是否违约\n",
       "0   738     1        0       0     3      1        1    1.0      5     0\n",
       "1   836     0        0       0     4      1        0    0.0      0     0\n",
       "2   825     0        0       0     4      1        0    0.0      0     0\n",
       "3    16     0        0       0     4      2        0    0.0      0     0\n",
       "4   825     1        0       0     5      1        0    0.0      0     0"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from imblearn.under_sampling  import NearMiss\n",
    "ee =NearMiss(version=1) # 设置为1，减少过拟合问题\n",
    "X_resampled,y_resampled = ee.fit_resample(ts,train_set['是否违约'])\n",
    "train_set1 =pd.concat([X_resampled,y_resampled],axis=1)\n",
    "train_set1.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>index</th>\n",
       "      <th>客户编号</th>\n",
       "      <th>已发货款</th>\n",
       "      <th>资产成本</th>\n",
       "      <th>贷款与资产比列</th>\n",
       "      <th>品牌</th>\n",
       "      <th>骑车销售商</th>\n",
       "      <th>车厂</th>\n",
       "      <th>出生日期</th>\n",
       "      <th>货款日期</th>\n",
       "      <th>...</th>\n",
       "      <th>已批准贷款总额</th>\n",
       "      <th>已发放贷款总额</th>\n",
       "      <th>每月还款总额</th>\n",
       "      <th>贷款与已还贷款比列</th>\n",
       "      <th>主账户还款期数</th>\n",
       "      <th>次账户还款期数</th>\n",
       "      <th>贷款与已批准贷款比列</th>\n",
       "      <th>总贷款次数与总有效贷款次数比</th>\n",
       "      <th>工作类型</th>\n",
       "      <th>是否违约</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>35882</td>\n",
       "      <td>619701</td>\n",
       "      <td>54413</td>\n",
       "      <td>66513</td>\n",
       "      <td>84.19</td>\n",
       "      <td>20</td>\n",
       "      <td>14004</td>\n",
       "      <td>86</td>\n",
       "      <td>1996</td>\n",
       "      <td>2018</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1551</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>35637</td>\n",
       "      <td>449077</td>\n",
       "      <td>56513</td>\n",
       "      <td>69000</td>\n",
       "      <td>83.33</td>\n",
       "      <td>2</td>\n",
       "      <td>15097</td>\n",
       "      <td>86</td>\n",
       "      <td>1979</td>\n",
       "      <td>2018</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>35666</td>\n",
       "      <td>424185</td>\n",
       "      <td>49487</td>\n",
       "      <td>63795</td>\n",
       "      <td>79.05</td>\n",
       "      <td>136</td>\n",
       "      <td>23384</td>\n",
       "      <td>86</td>\n",
       "      <td>1971</td>\n",
       "      <td>2018</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>35523</td>\n",
       "      <td>514276</td>\n",
       "      <td>55659</td>\n",
       "      <td>69095</td>\n",
       "      <td>82.35</td>\n",
       "      <td>19</td>\n",
       "      <td>15685</td>\n",
       "      <td>86</td>\n",
       "      <td>1985</td>\n",
       "      <td>2018</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>37016</td>\n",
       "      <td>546213</td>\n",
       "      <td>49703</td>\n",
       "      <td>66189</td>\n",
       "      <td>77.05</td>\n",
       "      <td>67</td>\n",
       "      <td>18166</td>\n",
       "      <td>86</td>\n",
       "      <td>1980</td>\n",
       "      <td>2018</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 50 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   index    客户编号   已发货款   资产成本  贷款与资产比列   品牌  骑车销售商  车厂  出生日期  货款日期  ...  \\\n",
       "0  35882  619701  54413  66513    84.19   20  14004  86  1996  2018  ...   \n",
       "1  35637  449077  56513  69000    83.33    2  15097  86  1979  2018  ...   \n",
       "2  35666  424185  49487  63795    79.05  136  23384  86  1971  2018  ...   \n",
       "3  35523  514276  55659  69095    82.35   19  15685  86  1985  2018  ...   \n",
       "4  37016  546213  49703  66189    77.05   67  18166  86  1980  2018  ...   \n",
       "\n",
       "   已批准贷款总额  已发放贷款总额  每月还款总额  贷款与已还贷款比列  主账户还款期数  次账户还款期数  贷款与已批准贷款比列  \\\n",
       "0        0        0    1551        1.0        0        0         1.0   \n",
       "1        0        0       0        1.0        0        0         1.0   \n",
       "2        0        0       0        1.0        0        0         1.0   \n",
       "3        0        0       0        1.0        0        0         1.0   \n",
       "4        0        0       0        1.0        0        0         1.0   \n",
       "\n",
       "   总贷款次数与总有效贷款次数比  工作类型  是否违约  \n",
       "0             1.0     0     0  \n",
       "1             1.0     0     0  \n",
       "2             1.0     0     0  \n",
       "3             1.0     0     0  \n",
       "4             1.0     1     0  \n",
       "\n",
       "[5 rows x 50 columns]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from imblearn.under_sampling  import NearMiss\n",
    "ee =NearMiss(version=1) # 设置为1，减少过拟合问题\n",
    "X_resampled, y_resampled = ee.fit_resample(ts2, user_info_worktype3['是否违约'])\n",
    "user_merchant =pd.concat([X_resampled,y_resampled],axis=1)\n",
    "user_merchant.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##  备选模型比较"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import time\n",
    "# 模型处理模块\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "# 标准化处理模块\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "# 常规模型\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "# 集成学习和stacking模型\n",
    "from sklearn.ensemble import AdaBoostClassifier, GradientBoostingClassifier, RandomForestClassifier\n",
    "# import xgboost as xgb\n",
    "# from xgboost.sklearn import XGBClassifier\n",
    "from mlxtend.classifier import StackingClassifier\n",
    "# 评价标准模块\n",
    "from sklearn import metrics\n",
    "from sklearn.metrics import accuracy_score,roc_auc_score,recall_score,precision_score\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts_x = train_set1.drop('是否违约',1) \n",
    "ts_y = train_set1['是否违约']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "X_train,X_test,y_train,y_test = train_test_split(ts_x,\n",
    "                                                 ts_y,test_size=0.3,random_state=6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "def train_model(X_train, y_train, X_test, y_test,\n",
    "               model,model_name):\n",
    "    \n",
    "    print('训练{}'.format(model_name))\n",
    "    \n",
    "    clf=model\n",
    "    start = time.time()\n",
    "    clf.fit(X_train, y_train.values.ravel())\n",
    "    \n",
    "     #验证模型\n",
    "    print('训练准确率：{:.4f}'.format(clf.score(X_train, y_train)))\n",
    "    \n",
    "    \n",
    "    predict=clf.predict(X_test)\n",
    "    score = clf.score(X_test, y_test)\n",
    "    precision=precision_score(y_test,predict)\n",
    "    recall=recall_score(y_test,predict)\n",
    "    print('测试准确率：{:.4f}'.format(score))\n",
    "    print('测试精确率：{:.4f}'.format(precision))\n",
    "    print('测试召回率：{:.4f}'.format(recall))\n",
    "    \n",
    "    end = time.time()\n",
    "    duration = end - start\n",
    "    print('模型训练耗时：{:6f}s'.format(duration))\n",
    "    \n",
    "    \n",
    "    return clf, score,precision,recall, duration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练LR\n",
      "训练准确率：0.7684\n",
      "测试准确率：0.7681\n",
      "测试精确率：0.8250\n",
      "测试召回率：0.6791\n",
      "模型训练耗时：0.362723s\n",
      "训练DT\n",
      "训练准确率：0.8572\n",
      "测试准确率：0.8361\n",
      "测试精确率：0.9229\n",
      "测试召回率：0.7324\n",
      "模型训练耗时：0.088761s\n",
      "训练AdaBoost\n",
      "训练准确率：0.8388\n",
      "测试准确率：0.8345\n",
      "测试精确率：0.8827\n",
      "测试召回率：0.7705\n",
      "模型训练耗时：0.667586s\n",
      "训练GBDT\n",
      "训练准确率：0.8487\n",
      "测试准确率：0.8427\n",
      "测试精确率：0.9099\n",
      "测试召回率：0.7597\n",
      "模型训练耗时：1.294411s\n",
      "训练RF\n",
      "训练准确率：0.8900\n",
      "测试准确率：0.8232\n",
      "测试精确率：0.8822\n",
      "测试召回率：0.7450\n",
      "模型训练耗时：1.715110s\n"
     ]
    }
   ],
   "source": [
    "model_name_param_dict = {    'LR': (LogisticRegression(penalty =\"l2\")),\n",
    "                             'DT': (DecisionTreeClassifier(max_depth=10,min_samples_split=10)),\n",
    "                             'AdaBoost': (AdaBoostClassifier()),\n",
    "                             'GBDT': (GradientBoostingClassifier()),\n",
    "                             'RF': (RandomForestClassifier()),\n",
    "                           \n",
    "                         }\n",
    "\n",
    "result_df = pd.DataFrame(columns=['Accuracy (%)','precision(%)','recall(%)','Time (s)'],\n",
    "                             index=list(model_name_param_dict.keys()))\n",
    "\n",
    "for model_name, model in model_name_param_dict.items():\n",
    "    clf, acc,pre,recall, mean_duration = train_model(X_train, y_train,\n",
    "                                                        X_test, y_test,\n",
    "                                                        model,model_name)\n",
    "    result_df.loc[model_name, 'Accuracy (%)'] = acc\n",
    "    result_df.loc[model_name, 'precision(%)'] = pre\n",
    "    result_df.loc[model_name, 'recall(%)'] = recall\n",
    "    result_df.loc[model_name, 'Time (s)'] = mean_duration \n",
    "\n",
    "result_df.to_csv(os.path.join('model_comparison.csv'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>index</th>\n",
       "      <th>客户编号</th>\n",
       "      <th>已发货款</th>\n",
       "      <th>资产成本</th>\n",
       "      <th>贷款与资产比列</th>\n",
       "      <th>品牌</th>\n",
       "      <th>骑车销售商</th>\n",
       "      <th>车厂</th>\n",
       "      <th>出生日期</th>\n",
       "      <th>货款日期</th>\n",
       "      <th>...</th>\n",
       "      <th>已批准贷款总额</th>\n",
       "      <th>已发放贷款总额</th>\n",
       "      <th>每月还款总额</th>\n",
       "      <th>贷款与已还贷款比列</th>\n",
       "      <th>主账户还款期数</th>\n",
       "      <th>次账户还款期数</th>\n",
       "      <th>贷款与已批准贷款比列</th>\n",
       "      <th>总贷款次数与总有效贷款次数比</th>\n",
       "      <th>工作类型</th>\n",
       "      <th>是否违约</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>32246.000000</td>\n",
       "      <td>32246.000000</td>\n",
       "      <td>32246.000000</td>\n",
       "      <td>32246.000000</td>\n",
       "      <td>32246.000000</td>\n",
       "      <td>32246.000000</td>\n",
       "      <td>32246.000000</td>\n",
       "      <td>32246.000000</td>\n",
       "      <td>32246.000000</td>\n",
       "      <td>32246.0</td>\n",
       "      <td>...</td>\n",
       "      <td>3.224600e+04</td>\n",
       "      <td>3.224600e+04</td>\n",
       "      <td>3.224600e+04</td>\n",
       "      <td>3.224600e+04</td>\n",
       "      <td>3.224600e+04</td>\n",
       "      <td>3.224600e+04</td>\n",
       "      <td>3.224600e+04</td>\n",
       "      <td>32246.000000</td>\n",
       "      <td>32246.000000</td>\n",
       "      <td>32246.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>38404.294641</td>\n",
       "      <td>539177.404701</td>\n",
       "      <td>54843.789090</td>\n",
       "      <td>74078.973950</td>\n",
       "      <td>76.966627</td>\n",
       "      <td>72.524871</td>\n",
       "      <td>19397.670967</td>\n",
       "      <td>70.092756</td>\n",
       "      <td>1982.473764</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>...</td>\n",
       "      <td>2.239183e+05</td>\n",
       "      <td>2.238882e+05</td>\n",
       "      <td>1.240579e+04</td>\n",
       "      <td>1.960114e+03</td>\n",
       "      <td>7.347653e+04</td>\n",
       "      <td>1.774601e+03</td>\n",
       "      <td>2.511630e+02</td>\n",
       "      <td>1.785791</td>\n",
       "      <td>0.442194</td>\n",
       "      <td>0.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>24119.184659</td>\n",
       "      <td>69405.022068</td>\n",
       "      <td>11671.511133</td>\n",
       "      <td>16416.457818</td>\n",
       "      <td>10.514729</td>\n",
       "      <td>67.764273</td>\n",
       "      <td>3499.712441</td>\n",
       "      <td>22.247313</td>\n",
       "      <td>9.322278</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>5.632939e+06</td>\n",
       "      <td>5.634631e+06</td>\n",
       "      <td>1.333528e+05</td>\n",
       "      <td>2.509667e+04</td>\n",
       "      <td>5.580765e+06</td>\n",
       "      <td>8.616640e+04</td>\n",
       "      <td>1.903673e+04</td>\n",
       "      <td>0.870745</td>\n",
       "      <td>0.496655</td>\n",
       "      <td>0.500008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>417428.000000</td>\n",
       "      <td>13990.000000</td>\n",
       "      <td>38055.000000</td>\n",
       "      <td>20.590000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>10524.000000</td>\n",
       "      <td>45.000000</td>\n",
       "      <td>1954.000000</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>17806.500000</td>\n",
       "      <td>478660.750000</td>\n",
       "      <td>48196.000000</td>\n",
       "      <td>65320.000000</td>\n",
       "      <td>71.940000</td>\n",
       "      <td>15.000000</td>\n",
       "      <td>16277.000000</td>\n",
       "      <td>48.000000</td>\n",
       "      <td>1976.000000</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>...</td>\n",
       "      <td>7.120000e+03</td>\n",
       "      <td>5.778250e+03</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>1.170000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>35427.000000</td>\n",
       "      <td>541725.500000</td>\n",
       "      <td>54318.500000</td>\n",
       "      <td>70158.000000</td>\n",
       "      <td>79.130000</td>\n",
       "      <td>64.000000</td>\n",
       "      <td>18535.000000</td>\n",
       "      <td>86.000000</td>\n",
       "      <td>1984.000000</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>...</td>\n",
       "      <td>4.144150e+04</td>\n",
       "      <td>4.090500e+04</td>\n",
       "      <td>1.760000e+03</td>\n",
       "      <td>1.240000e+00</td>\n",
       "      <td>1.400000e+01</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>1.550000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>58023.250000</td>\n",
       "      <td>599584.250000</td>\n",
       "      <td>60713.000000</td>\n",
       "      <td>77145.000000</td>\n",
       "      <td>84.850000</td>\n",
       "      <td>130.000000</td>\n",
       "      <td>22917.000000</td>\n",
       "      <td>86.000000</td>\n",
       "      <td>1990.000000</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>...</td>\n",
       "      <td>1.129000e+05</td>\n",
       "      <td>1.122798e+05</td>\n",
       "      <td>5.656000e+03</td>\n",
       "      <td>2.060000e+00</td>\n",
       "      <td>8.645000e+02</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>90377.000000</td>\n",
       "      <td>658664.000000</td>\n",
       "      <td>191392.000000</td>\n",
       "      <td>281164.000000</td>\n",
       "      <td>95.000000</td>\n",
       "      <td>261.000000</td>\n",
       "      <td>24803.000000</td>\n",
       "      <td>153.000000</td>\n",
       "      <td>1997.000000</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>...</td>\n",
       "      <td>1.000000e+09</td>\n",
       "      <td>1.000000e+09</td>\n",
       "      <td>1.542041e+07</td>\n",
       "      <td>1.767001e+06</td>\n",
       "      <td>1.000000e+09</td>\n",
       "      <td>1.100000e+07</td>\n",
       "      <td>3.000001e+06</td>\n",
       "      <td>18.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8 rows × 50 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "              index           客户编号           已发货款           资产成本  \\\n",
       "count  32246.000000   32246.000000   32246.000000   32246.000000   \n",
       "mean   38404.294641  539177.404701   54843.789090   74078.973950   \n",
       "std    24119.184659   69405.022068   11671.511133   16416.457818   \n",
       "min        1.000000  417428.000000   13990.000000   38055.000000   \n",
       "25%    17806.500000  478660.750000   48196.000000   65320.000000   \n",
       "50%    35427.000000  541725.500000   54318.500000   70158.000000   \n",
       "75%    58023.250000  599584.250000   60713.000000   77145.000000   \n",
       "max    90377.000000  658664.000000  191392.000000  281164.000000   \n",
       "\n",
       "            贷款与资产比列            品牌         骑车销售商            车厂          出生日期  \\\n",
       "count  32246.000000  32246.000000  32246.000000  32246.000000  32246.000000   \n",
       "mean      76.966627     72.524871  19397.670967     70.092756   1982.473764   \n",
       "std       10.514729     67.764273   3499.712441     22.247313      9.322278   \n",
       "min       20.590000      1.000000  10524.000000     45.000000   1954.000000   \n",
       "25%       71.940000     15.000000  16277.000000     48.000000   1976.000000   \n",
       "50%       79.130000     64.000000  18535.000000     86.000000   1984.000000   \n",
       "75%       84.850000    130.000000  22917.000000     86.000000   1990.000000   \n",
       "max       95.000000    261.000000  24803.000000    153.000000   1997.000000   \n",
       "\n",
       "          货款日期  ...       已批准贷款总额       已发放贷款总额        每月还款总额     贷款与已还贷款比列  \\\n",
       "count  32246.0  ...  3.224600e+04  3.224600e+04  3.224600e+04  3.224600e+04   \n",
       "mean    2018.0  ...  2.239183e+05  2.238882e+05  1.240579e+04  1.960114e+03   \n",
       "std        0.0  ...  5.632939e+06  5.634631e+06  1.333528e+05  2.509667e+04   \n",
       "min     2018.0  ...  0.000000e+00  0.000000e+00  0.000000e+00  1.000000e+00   \n",
       "25%     2018.0  ...  7.120000e+03  5.778250e+03  0.000000e+00  1.000000e+00   \n",
       "50%     2018.0  ...  4.144150e+04  4.090500e+04  1.760000e+03  1.240000e+00   \n",
       "75%     2018.0  ...  1.129000e+05  1.122798e+05  5.656000e+03  2.060000e+00   \n",
       "max     2018.0  ...  1.000000e+09  1.000000e+09  1.542041e+07  1.767001e+06   \n",
       "\n",
       "            主账户还款期数       次账户还款期数    贷款与已批准贷款比列  总贷款次数与总有效贷款次数比          工作类型  \\\n",
       "count  3.224600e+04  3.224600e+04  3.224600e+04    32246.000000  32246.000000   \n",
       "mean   7.347653e+04  1.774601e+03  2.511630e+02        1.785791      0.442194   \n",
       "std    5.580765e+06  8.616640e+04  1.903673e+04        0.870745      0.496655   \n",
       "min    0.000000e+00  0.000000e+00  0.000000e+00        1.000000      0.000000   \n",
       "25%    1.000000e+00  0.000000e+00  1.000000e+00        1.170000      0.000000   \n",
       "50%    1.400000e+01  0.000000e+00  1.000000e+00        1.550000      0.000000   \n",
       "75%    8.645000e+02  0.000000e+00  1.000000e+00        2.000000      1.000000   \n",
       "max    1.000000e+09  1.100000e+07  3.000001e+06       18.000000      1.000000   \n",
       "\n",
       "               是否违约  \n",
       "count  32246.000000  \n",
       "mean       0.500000  \n",
       "std        0.500008  \n",
       "min        0.000000  \n",
       "25%        0.000000  \n",
       "50%        0.500000  \n",
       "75%        1.000000  \n",
       "max        1.000000  \n",
       "\n",
       "[8 rows x 50 columns]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_merchant.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>信用评分</th>\n",
       "      <th>工作类型</th>\n",
       "      <th>是否出具驾驶证</th>\n",
       "      <th>是否填写护照</th>\n",
       "      <th>年岭区间</th>\n",
       "      <th>总贷款次数</th>\n",
       "      <th>总有效贷款次数</th>\n",
       "      <th>贷款成功率</th>\n",
       "      <th>总还款期数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.612364</td>\n",
       "      <td>1.135726</td>\n",
       "      <td>-0.125244</td>\n",
       "      <td>-0.033432</td>\n",
       "      <td>-0.526908</td>\n",
       "      <td>-0.396119</td>\n",
       "      <td>-0.098743</td>\n",
       "      <td>1.390123</td>\n",
       "      <td>-0.011638</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.943768</td>\n",
       "      <td>-0.880494</td>\n",
       "      <td>-0.125244</td>\n",
       "      <td>-0.033432</td>\n",
       "      <td>0.224194</td>\n",
       "      <td>-0.396119</td>\n",
       "      <td>-0.689148</td>\n",
       "      <td>-0.988054</td>\n",
       "      <td>-0.011639</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.906569</td>\n",
       "      <td>-0.880494</td>\n",
       "      <td>-0.125244</td>\n",
       "      <td>-0.033432</td>\n",
       "      <td>0.224194</td>\n",
       "      <td>-0.396119</td>\n",
       "      <td>-0.689148</td>\n",
       "      <td>-0.988054</td>\n",
       "      <td>-0.011639</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-1.829205</td>\n",
       "      <td>-0.880494</td>\n",
       "      <td>-0.125244</td>\n",
       "      <td>-0.033432</td>\n",
       "      <td>0.224194</td>\n",
       "      <td>-0.206756</td>\n",
       "      <td>-0.689148</td>\n",
       "      <td>-0.988054</td>\n",
       "      <td>-0.011639</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.906569</td>\n",
       "      <td>1.135726</td>\n",
       "      <td>-0.125244</td>\n",
       "      <td>-0.033432</td>\n",
       "      <td>0.975296</td>\n",
       "      <td>-0.396119</td>\n",
       "      <td>-0.689148</td>\n",
       "      <td>-0.988054</td>\n",
       "      <td>-0.011639</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32241</th>\n",
       "      <td>-0.757214</td>\n",
       "      <td>-0.880494</td>\n",
       "      <td>-0.125244</td>\n",
       "      <td>-0.033432</td>\n",
       "      <td>0.224194</td>\n",
       "      <td>1.118779</td>\n",
       "      <td>-0.098743</td>\n",
       "      <td>-0.723838</td>\n",
       "      <td>-0.011638</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32242</th>\n",
       "      <td>-0.669291</td>\n",
       "      <td>1.135726</td>\n",
       "      <td>-0.125244</td>\n",
       "      <td>-0.033432</td>\n",
       "      <td>0.975296</td>\n",
       "      <td>0.171968</td>\n",
       "      <td>0.491663</td>\n",
       "      <td>0.201035</td>\n",
       "      <td>-0.011638</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32243</th>\n",
       "      <td>0.608982</td>\n",
       "      <td>1.135726</td>\n",
       "      <td>-0.125244</td>\n",
       "      <td>-0.033432</td>\n",
       "      <td>0.224194</td>\n",
       "      <td>-0.396119</td>\n",
       "      <td>-0.098743</td>\n",
       "      <td>1.390123</td>\n",
       "      <td>-0.011634</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32244</th>\n",
       "      <td>-0.033536</td>\n",
       "      <td>-0.880494</td>\n",
       "      <td>-0.125244</td>\n",
       "      <td>-0.033432</td>\n",
       "      <td>0.224194</td>\n",
       "      <td>2.823039</td>\n",
       "      <td>4.034097</td>\n",
       "      <td>0.068808</td>\n",
       "      <td>-0.011634</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32245</th>\n",
       "      <td>-0.473154</td>\n",
       "      <td>-0.880494</td>\n",
       "      <td>-0.125244</td>\n",
       "      <td>-0.033432</td>\n",
       "      <td>0.975296</td>\n",
       "      <td>2.633677</td>\n",
       "      <td>0.491663</td>\n",
       "      <td>-0.708380</td>\n",
       "      <td>-0.011638</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>32246 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           信用评分      工作类型   是否出具驾驶证    是否填写护照      年岭区间     总贷款次数   总有效贷款次数  \\\n",
       "0      0.612364  1.135726 -0.125244 -0.033432 -0.526908 -0.396119 -0.098743   \n",
       "1      0.943768 -0.880494 -0.125244 -0.033432  0.224194 -0.396119 -0.689148   \n",
       "2      0.906569 -0.880494 -0.125244 -0.033432  0.224194 -0.396119 -0.689148   \n",
       "3     -1.829205 -0.880494 -0.125244 -0.033432  0.224194 -0.206756 -0.689148   \n",
       "4      0.906569  1.135726 -0.125244 -0.033432  0.975296 -0.396119 -0.689148   \n",
       "...         ...       ...       ...       ...       ...       ...       ...   \n",
       "32241 -0.757214 -0.880494 -0.125244 -0.033432  0.224194  1.118779 -0.098743   \n",
       "32242 -0.669291  1.135726 -0.125244 -0.033432  0.975296  0.171968  0.491663   \n",
       "32243  0.608982  1.135726 -0.125244 -0.033432  0.224194 -0.396119 -0.098743   \n",
       "32244 -0.033536 -0.880494 -0.125244 -0.033432  0.224194  2.823039  4.034097   \n",
       "32245 -0.473154 -0.880494 -0.125244 -0.033432  0.975296  2.633677  0.491663   \n",
       "\n",
       "          贷款成功率     总还款期数  \n",
       "0      1.390123 -0.011638  \n",
       "1     -0.988054 -0.011639  \n",
       "2     -0.988054 -0.011639  \n",
       "3     -0.988054 -0.011639  \n",
       "4     -0.988054 -0.011639  \n",
       "...         ...       ...  \n",
       "32241 -0.723838 -0.011638  \n",
       "32242  0.201035 -0.011638  \n",
       "32243  1.390123 -0.011634  \n",
       "32244  0.068808 -0.011634  \n",
       "32245 -0.708380 -0.011638  \n",
       "\n",
       "[32246 rows x 9 columns]"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 归一化\n",
    "from sklearn.preprocessing import StandardScaler \n",
    "\n",
    "\n",
    "scaler = StandardScaler() \n",
    "ts_x_std = pd.DataFrame(scaler.fit_transform(ts_x),columns = ts_x.columns)\n",
    "ts_x_std\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "X_train,X_test,y_train,y_test = train_test_split(ts_x_std,\n",
    "                                                 ts_y,test_size=0.3,random_state=6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练LR\n",
      "训练准确率：0.7823\n",
      "测试准确率：0.7781\n",
      "测试精确率：0.7782\n",
      "测试召回率：0.7761\n",
      "模型训练耗时：0.071778s\n",
      "训练DT\n",
      "训练准确率：0.8572\n",
      "测试准确率：0.8361\n",
      "测试精确率：0.9238\n",
      "测试召回率：0.7316\n",
      "模型训练耗时：0.039895s\n",
      "训练AdaBoost\n",
      "训练准确率：0.8388\n",
      "测试准确率：0.8345\n",
      "测试精确率：0.8827\n",
      "测试召回率：0.7705\n",
      "模型训练耗时：0.656275s\n",
      "训练GBDT\n",
      "训练准确率：0.8487\n",
      "测试准确率：0.8427\n",
      "测试精确率：0.9099\n",
      "测试召回率：0.7597\n",
      "模型训练耗时：1.259659s\n",
      "训练RF\n",
      "训练准确率：0.8900\n",
      "测试准确率：0.8214\n",
      "测试精确率：0.8805\n",
      "测试召回率：0.7425\n",
      "模型训练耗时：1.700044s\n"
     ]
    }
   ],
   "source": [
    "model_name_param_dict = {    'LR': (LogisticRegression(penalty =\"l2\")),\n",
    "                             'DT': (DecisionTreeClassifier(max_depth=10,min_samples_split=10)),\n",
    "                             'AdaBoost': (AdaBoostClassifier()),\n",
    "                             'GBDT': (GradientBoostingClassifier()),\n",
    "                             'RF': (RandomForestClassifier()),\n",
    "                           \n",
    "                         }\n",
    "\n",
    "result_df = pd.DataFrame(columns=['Accuracy (%)','precision(%)','recall(%)','Time (s)'],\n",
    "                             index=list(model_name_param_dict.keys()))\n",
    "\n",
    "for model_name, model in model_name_param_dict.items():\n",
    "    clf, acc,pre,recall, mean_duration = train_model(X_train, y_train,\n",
    "                                                        X_test, y_test,\n",
    "                                                        model,model_name)\n",
    "    result_df.loc[model_name, 'Accuracy (%)'] = acc\n",
    "    result_df.loc[model_name, 'precision(%)'] = pre\n",
    "    result_df.loc[model_name, 'recall(%)'] = recall\n",
    "    result_df.loc[model_name, 'Time (s)'] = mean_duration \n",
    "\n",
    "result_df.to_csv(os.path.join('model_comparison.csv'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 网格搜索调优"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.ensemble import RandomForestClassifier\n",
    "\n",
    "randomforest = RandomForestClassifier()\n",
    "\n",
    "# model = randomforest.fit(ts_x_std ,ts_y)\n",
    "\n",
    "\n",
    "from sklearn.model_selection import GridSearchCV \n",
    "\n",
    "p = {\n",
    "    \n",
    "    'max_depth': range(6,10),\n",
    "    'n_estimators' : range(10,30,5)\n",
    "    \n",
    "    \n",
    "}\n",
    "GS = GridSearchCV(randomforest,p,cv =3 )\n",
    "result = GS.fit(X_train ,y_train)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=3, estimator=RandomForestClassifier(),\n",
       "             param_grid={'max_depth': range(6, 10),\n",
       "                         'n_estimators': range(10, 30, 5)})"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "GS.fit(ts_x_std ,ts_y)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 0, 0, ..., 1, 0, 1], dtype=int64)"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pre = result.predict(X_test)\n",
    "pre"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(           信用评分      工作类型   是否出具驾驶证    是否填写护照      年岭区间     总贷款次数   总有效贷款次数  \\\n",
       " 4185   0.906569  1.135726 -0.125244 -0.033432  0.975296 -0.396119 -0.689148   \n",
       " 2794   0.943768 -0.880494 -0.125244 -0.033432  1.726399 -0.396119 -0.689148   \n",
       " 9025  -1.829205 -0.880494 -0.125244 -0.033432  0.224194 -0.396119 -0.689148   \n",
       " 13096  0.943768  1.135726 -0.125244 -0.033432 -0.526908 -0.206756 -0.689148   \n",
       " 2768   0.906569 -0.880494 -0.125244 -0.033432  0.224194 -0.396119 -0.689148   \n",
       " ...         ...       ...       ...       ...       ...       ...       ...   \n",
       " 4714   0.930241 -0.880494 -0.125244 -0.033432  0.224194 -0.206756 -0.689148   \n",
       " 10196 -1.829205 -0.880494 -0.125244 -0.033432  0.224194 -0.396119 -0.098743   \n",
       " 8419   0.612364 -0.880494 -0.125244 -0.033432 -2.029113 -0.396119 -0.098743   \n",
       " 19145 -1.829205  1.135726 -0.125244 -0.033432  0.224194  1.686866 -0.689148   \n",
       " 31626  0.906569  1.135726 -0.125244 -0.033432  0.975296  0.929417 -0.689148   \n",
       " \n",
       "           贷款成功率     总还款期数  \n",
       " 4185  -0.988054 -0.011639  \n",
       " 2794  -0.988054 -0.011639  \n",
       " 9025  -0.988054 -0.011639  \n",
       " 13096 -0.988054 -0.011639  \n",
       " 2768  -0.988054 -0.011639  \n",
       " ...         ...       ...  \n",
       " 4714  -0.988054 -0.011639  \n",
       " 10196  1.390123 -0.011639  \n",
       " 8419   1.390123 -0.011638  \n",
       " 19145 -0.988054 -0.011639  \n",
       " 31626 -0.988054 -0.011639  \n",
       " \n",
       " [22572 rows x 9 columns],\n",
       "            信用评分      工作类型   是否出具驾驶证    是否填写护照      年岭区间     总贷款次数   总有效贷款次数  \\\n",
       " 20140  0.612364  1.135726 -0.125244 -0.033432  0.224194 -0.017394 -0.098743   \n",
       " 14528  0.189654  1.135726 -0.125244 -0.033432  0.224194 -0.396119 -0.689148   \n",
       " 6467   0.612364  1.135726 -0.125244 -0.033432 -0.526908 -0.206756 -0.098743   \n",
       " 29083 -0.868810  1.135726 -0.125244 -0.033432  0.975296  0.929417  0.491663   \n",
       " 26901 -1.829205  1.135726 -0.125244 -0.033432 -0.526908 -0.396119 -0.689148   \n",
       " ...         ...       ...       ...       ...       ...       ...       ...   \n",
       " 2232  -1.829205  1.135726 -0.125244 -0.033432 -0.526908 -0.396119 -0.689148   \n",
       " 3301   0.696905  1.135726 -0.125244 -0.033432 -0.526908 -0.396119 -0.098743   \n",
       " 30988  0.382410  1.135726 -0.125244 -0.033432  0.224194 -0.206756 -0.098743   \n",
       " 9823   0.923478  1.135726 -0.125244 -0.033432  0.224194 -0.396119 -0.689148   \n",
       " 28011  0.057769 -0.880494 -0.125244 -0.033432 -0.526908 -0.017394  0.491663   \n",
       " \n",
       "           贷款成功率     总还款期数  \n",
       " 20140 -0.195407 -0.011638  \n",
       " 14528 -0.988054 -0.011639  \n",
       " 6467   0.201035 -0.011638  \n",
       " 29083 -0.393509  0.176691  \n",
       " 26901 -0.988054 -0.011639  \n",
       " ...         ...       ...  \n",
       " 2232  -0.988054 -0.011639  \n",
       " 3301   1.390123 -0.011638  \n",
       " 30988  0.201035 -0.011632  \n",
       " 9823  -0.988054 -0.011639  \n",
       " 28011  0.597477 -0.011634  \n",
       " \n",
       " [9674 rows x 9 columns],\n",
       " 4185     0\n",
       " 2794     0\n",
       " 9025     0\n",
       " 13096    0\n",
       " 2768     0\n",
       "         ..\n",
       " 4714     0\n",
       " 10196    0\n",
       " 8419     0\n",
       " 19145    1\n",
       " 31626    1\n",
       " Name: 是否违约, Length: 22572, dtype: int64,\n",
       " 20140    1\n",
       " 14528    0\n",
       " 6467     0\n",
       " 29083    1\n",
       " 26901    1\n",
       "         ..\n",
       " 2232     0\n",
       " 3301     0\n",
       " 30988    1\n",
       " 9823     0\n",
       " 28011    1\n",
       " Name: 是否违约, Length: 9674, dtype: int64)"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train,X_test,y_train,y_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.8783847980997624 0.7665837479270315\n"
     ]
    }
   ],
   "source": [
    "print(precision_score(y_test,pre),recall_score(y_test,pre))\n",
    "# 精准率上升  召回率下降"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 优质模型保存"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['model.model']"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import joblib\n",
    "# 保存模型\n",
    "joblib.dump(result,'model.model')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 加载模型\n",
    "# clf = joblib.load('model.model')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
